Skip to content

Commit

Permalink
Merge pull request jenkinsci#463 from nestoracunablanco/feature/verif…
Browse files Browse the repository at this point in the history
…yMethod

Adds verify method in BasePipelineTest.
  • Loading branch information
nre-ableton authored Jan 10, 2022
2 parents 8906c47 + 2036a79 commit ee31dd9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import static org.assertj.core.api.Assertions.assertThat

import org.assertj.core.api.AbstractCharSequenceAssert

import groovy.transform.Memoized

abstract class BasePipelineTest {

PipelineTestHelper helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,25 @@ class PipelineTestHelper {
}.count()
}

/**
* Verifies if a method was called, with the preconditions defined in times and methodVerification, if wanted.
* @param name the method name
* @param times times the method shall be called.
* @param methodVerification a closure with the a MethodSignature object as input parameter, which verifies a condition
*/
void verify(String name, int times = 1, Closure methodVerification = { return true }) {
List<MethodCall> methodCalls = callStack.findAll { it.getMethodName() == name }
methodCalls.each { call ->
if (!methodVerification(call)) {
throw new VerificationException("Method call $call failed to be verified")
}
}
int timesCalled = methodCalls.size()
if (times != timesCalled) {
throw new VerificationException("Expected method $name to be called $times times, but was called $timesCalled times")
}
}

/**
* Call closure by handling spreading of parameter default values
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.lesfurets.jenkins.unit

/**
* Custom exception class used in the verify method in BasePipelineTest
*/
class VerificationException extends Exception {

VerificationException(String errorMessage) {
super(errorMessage)
}
}
72 changes: 72 additions & 0 deletions src/test/groovy/com/lesfurets/jenkins/unit/VerifyTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.lesfurets.jenkins.unit

import org.junit.Before
import org.junit.Test

class VerifyTest extends BasePipelineTest {

static final String PARAMETER1 = "someString"
static final String PARAMETER2 = "anotherString"
static final String SOME_STRING_METHOD_NAME = "someStringMethod"
static final String VOID_METHOD_NAME = "voidMethod"

Script script

@Before
void beforeTest() {
setUp()
script = loadScript('src/test/jenkins/job/verify.jenkins')
}

@Test
void verify_some_string_method() {
script.someStringMethod(PARAMETER1)
script.someStringMethod(PARAMETER1, PARAMETER2)
helper.verify(SOME_STRING_METHOD_NAME, 2)
}

@Test
void verify_some_string_method_parameter1() {
script.someStringMethod(PARAMETER1)
helper.verify(SOME_STRING_METHOD_NAME, 1) { methodCall ->
return methodCall.args[0].toString() == PARAMETER1
}
}

@Test
void verify_some_string_method_parameter2() {
script.someStringMethod(PARAMETER1, PARAMETER2)
helper.verify(SOME_STRING_METHOD_NAME, 1) { methodCall ->
Object[] arguments = methodCall.args
return arguments.size() == 2 && arguments[0].toString() == PARAMETER1 && arguments[1].toString() == PARAMETER2
}
}

@Test(expected = VerificationException.class)
void verify_some_string_method_another_params() {
script.someStringMethod(PARAMETER1, "another")
helper.verify(SOME_STRING_METHOD_NAME, 1) { MethodCall methodCall ->
Object[] arguments = methodCall.args
return arguments.size() == 2 && arguments[0].toString() == PARAMETER1 && arguments[1].toString() == PARAMETER2
}
}

@Test
void verify_void_method() {
script.voidMethod()
helper.verify(VOID_METHOD_NAME)
}

@Test(expected = VerificationException.class)
void verify_void_method_expect_param() {
script.voidMethod()
helper.verify(VOID_METHOD_NAME, 1) { methodCall -> methodCall.args.size() > 0 }
}

@Test(expected = VerificationException.class)
void verify_void_method_less_times() {
script.voidMethod()
script.voidMethod()
helper.verify(VOID_METHOD_NAME)
}
}
7 changes: 7 additions & 0 deletions src/test/jenkins/job/verify.jenkins
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def someStringMethod(String someString1, String someString2="") {
return someString1 + someString2
}

def voidMethod() {
echo "some output"
}

0 comments on commit ee31dd9

Please sign in to comment.