Skip to content

Instantly share code, notes, and snippets.

@solvingj
Created June 24, 2020 13:48
Show Gist options
  • Select an option

  • Save solvingj/87851300eb97e5aaab524d3e12eee956 to your computer and use it in GitHub Desktop.

Select an option

Save solvingj/87851300eb97e5aaab524d3e12eee956 to your computer and use it in GitHub Desktop.
JPU setup error
package demo.ci.arg
import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest
import org.junit.Before
import org.junit.jupiter.api.Test
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
class ConanArgsJPUSpec extends DeclarativePipelineTest {
@Override
@Before
void setUp() throws Exception {
super.setUp()
def library = library().name('demo_jenkins_library')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
helper.registerSharedLibrary(library)
}
@Test
void should_execute_without_errors() throws Exception {
setUp() // I should not have to do this. super.setup() in setup() should be enough
// If i remove, I get error: Helper is not initialized: Call setUp() before tests.
File jenkinsfile = new File('test/resources/jenkinsfiles/Jenkinsfile')
def script = loadScript(jenkinsfile.path)
runScript(script)
printCallStack()
}
}
@stchar

stchar commented Jun 28, 2020

Copy link
Copy Markdown

line 26 is not needed. 13 will do the job
28,29,30 could be replaced with

runScript('test/resources/jenkinsfiles/Jenkinsfile')

@solvingj

Copy link
Copy Markdown
Author

Thanks. 13 doesn't do the job... thats the problem. I get JPU setup error if I remove it.

@stchar

stchar commented Jun 30, 2020

Copy link
Copy Markdown

Sorry, I don't quite follow. What are the benefits to remove super.setUp()?

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

Ok I think I got it. You are confused with super.setUp() aren't you.

If you take a look at sources of DeclarativePipelineTest it extends with BasePipelineTest which has a method setUp
https://github.com/jenkinsci/JenkinsPipelineUnit/blob/3d0c6c2d1b2969e571fb2045580128c98bb59d95/src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy#L47

This method initializes basic utility objects to load shared libs, pipeline scrips and some mocks of widely used pipeline plugins

This is very important method but Junit won't call

That's why you have to declare a method in your test class to be decorated with @Before and to call setUp of the base class.

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

That's why you must not to remove line 13

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

Shall you remove it and essential JPU features aren't loaded, and you get exception "Helper is not initialized"

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

Btw are you sure in import org.junit.jupiter.api.Test
Usually it is import org.junit.Test

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

What's the version of Junit are you using 4 or 5?

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

If 5 it doesn't have Before. It has to be BeforeEach or BeforeAll
https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/package-summary.html

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

JPU was designed to be used within Junit4 most of examples are for that version of Junit

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

But it doesn't mean it can't be used with Junit5

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

Unless some assertions depends on junit4 but it's not a big deal to override it

@solvingj

solvingj commented Jul 2, 2020

Copy link
Copy Markdown
Author

Ok, yes, it's JUnit 5. I had no idea there was no @Before. That seems to explain the confusion.

I just tried and added:

    @BeforeEach
    void jpuJunit4Setup(){
        setUp()
    }

As a hacky workaround and now i don't need to call setUp() in all my tests anymore. Thanks!

@stchar

stchar commented Jul 2, 2020

Copy link
Copy Markdown

cool, good to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment