Skip to content

Instantly share code, notes, and snippets.

@solvingj
Created June 24, 2020 13:48
Show Gist options
  • Save solvingj/87851300eb97e5aaab524d3e12eee956 to your computer and use it in GitHub Desktop.
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
Copy link

stchar commented Jun 28, 2020

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
Author

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

@stchar
Copy link

stchar commented Jun 30, 2020

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

@stchar
Copy link

stchar commented Jul 2, 2020

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
Copy link

stchar commented Jul 2, 2020

That's why you must not to remove line 13

@stchar
Copy link

stchar commented Jul 2, 2020

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

@stchar
Copy link

stchar commented Jul 2, 2020

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

@stchar
Copy link

stchar commented Jul 2, 2020

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

@stchar
Copy link

stchar commented Jul 2, 2020

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
Copy link

stchar commented Jul 2, 2020

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

@stchar
Copy link

stchar commented Jul 2, 2020

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

@stchar
Copy link

stchar commented Jul 2, 2020

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

@solvingj
Copy link
Author

solvingj commented Jul 2, 2020

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
Copy link

stchar commented Jul 2, 2020

cool, good to know!

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