-
-
Save solvingj/87851300eb97e5aaab524d3e12eee956 to your computer and use it in GitHub Desktop.
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() | |
} | |
} |
Thanks. 13 doesn't do the job... thats the problem. I get JPU setup error if I remove it.
Sorry, I don't quite follow. What are the benefits to remove super.setUp()
?
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.
That's why you must not to remove line 13
Shall you remove it and essential JPU features aren't loaded, and you get exception "Helper is not initialized"
Btw are you sure in import org.junit.jupiter.api.Test
Usually it is import org.junit.Test
What's the version of Junit are you using 4 or 5?
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
JPU was designed to be used within Junit4 most of examples are for that version of Junit
But it doesn't mean it can't be used with Junit5
Unless some assertions depends on junit4 but it's not a big deal to override it
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!
cool, good to know!
line 26 is not needed. 13 will do the job
28,29,30 could be replaced with
runScript('test/resources/jenkinsfiles/Jenkinsfile')