DEV Community

Discussion on: How-To: Setup a unit-testable Jenkins shared pipeline library

Collapse
 
kuperadrian profile image
Adrian Kuper

You are using a Scripted Pipeline, right? Not sure how you could mock the node step (or any step that uses closures for that matter) as I'm only familiar with Declarative Pipelines. Sorry 🙈

Keep in mind that the goal of this article is to test your Jenkins Pipeline Library. Not the pipeline itself (aka the Jenkinsfile).

Collapse
 
biskit1943 profile image
Maximilian Konter • Edited

Thank you for the reply.
Yes I'm using the Scripted Pipeline, but I use my SL to do some things on nodes. Anyway, I figured it out just now.

The following is my current code to mock node("<name>") {}:

// StepExecutor.groovy
@Override
void node(String name, Closure closure) {
    this._steps.node(name, closure)
}
Enter fullscreen mode Exit fullscreen mode
// SomeTest.groovy
@Before
void setUp() {
    doAnswer({ invocation ->
        String name = invocation.getArgument(0)
        Closure closure = invocation.getArgument(1)

        println(name)
        closure.call()
    }).when(_steps).node(anyString(), any())
}
Enter fullscreen mode Exit fullscreen mode

My problem was to figure out what signature the method node had. This was
caused by the fact, that I did not know, that node("<name>") {} is the
same as node("<name>", {}).

So for anyone wondering how to mock the closure methods, this is the way.

@kuperadrian maybe you could include this in your article to help anyone who is in need of mocking a closure :)