Skip to content

Commit 2036a79

Browse files
author
Nestor Acuna-Blanco
committed
Adds verify method in BasePipelineTest.
The idea behind is to provide a similar functionality to Mockito.verify: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#1 When the verification fails, a VerificationException will be thrown, making the explicit assertions in the unit tests less necessary.
1 parent a61c471 commit 2036a79

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

src/main/groovy/com/lesfurets/jenkins/unit/BasePipelineTest.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import static org.assertj.core.api.Assertions.assertThat
55

66
import org.assertj.core.api.AbstractCharSequenceAssert
77

8-
import groovy.transform.Memoized
9-
108
abstract class BasePipelineTest {
119

1210
PipelineTestHelper helper

src/main/groovy/com/lesfurets/jenkins/unit/PipelineTestHelper.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,25 @@ class PipelineTestHelper {
595595
}.count()
596596
}
597597

598+
/**
599+
* Verifies if a method was called, with the preconditions defined in times and methodVerification, if wanted.
600+
* @param name the method name
601+
* @param times times the method shall be called.
602+
* @param methodVerification a closure with the a MethodSignature object as input parameter, which verifies a condition
603+
*/
604+
void verify(String name, int times = 1, Closure methodVerification = { return true }) {
605+
List<MethodCall> methodCalls = callStack.findAll { it.getMethodName() == name }
606+
methodCalls.each { call ->
607+
if (!methodVerification(call)) {
608+
throw new VerificationException("Method call $call failed to be verified")
609+
}
610+
}
611+
int timesCalled = methodCalls.size()
612+
if (times != timesCalled) {
613+
throw new VerificationException("Expected method $name to be called $times times, but was called $timesCalled times")
614+
}
615+
}
616+
598617
/**
599618
* Call closure by handling spreading of parameter default values
600619
*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.lesfurets.jenkins.unit
2+
3+
/**
4+
* Custom exception class used in the verify method in BasePipelineTest
5+
*/
6+
class VerificationException extends Exception {
7+
8+
VerificationException(String errorMessage) {
9+
super(errorMessage)
10+
}
11+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.lesfurets.jenkins.unit
2+
3+
import org.junit.Before
4+
import org.junit.Test
5+
6+
class VerifyTest extends BasePipelineTest {
7+
8+
static final String PARAMETER1 = "someString"
9+
static final String PARAMETER2 = "anotherString"
10+
static final String SOME_STRING_METHOD_NAME = "someStringMethod"
11+
static final String VOID_METHOD_NAME = "voidMethod"
12+
13+
Script script
14+
15+
@Before
16+
void beforeTest() {
17+
setUp()
18+
script = loadScript('src/test/jenkins/job/verify.jenkins')
19+
}
20+
21+
@Test
22+
void verify_some_string_method() {
23+
script.someStringMethod(PARAMETER1)
24+
script.someStringMethod(PARAMETER1, PARAMETER2)
25+
helper.verify(SOME_STRING_METHOD_NAME, 2)
26+
}
27+
28+
@Test
29+
void verify_some_string_method_parameter1() {
30+
script.someStringMethod(PARAMETER1)
31+
helper.verify(SOME_STRING_METHOD_NAME, 1) { methodCall ->
32+
return methodCall.args[0].toString() == PARAMETER1
33+
}
34+
}
35+
36+
@Test
37+
void verify_some_string_method_parameter2() {
38+
script.someStringMethod(PARAMETER1, PARAMETER2)
39+
helper.verify(SOME_STRING_METHOD_NAME, 1) { methodCall ->
40+
Object[] arguments = methodCall.args
41+
return arguments.size() == 2 && arguments[0].toString() == PARAMETER1 && arguments[1].toString() == PARAMETER2
42+
}
43+
}
44+
45+
@Test(expected = VerificationException.class)
46+
void verify_some_string_method_another_params() {
47+
script.someStringMethod(PARAMETER1, "another")
48+
helper.verify(SOME_STRING_METHOD_NAME, 1) { MethodCall methodCall ->
49+
Object[] arguments = methodCall.args
50+
return arguments.size() == 2 && arguments[0].toString() == PARAMETER1 && arguments[1].toString() == PARAMETER2
51+
}
52+
}
53+
54+
@Test
55+
void verify_void_method() {
56+
script.voidMethod()
57+
helper.verify(VOID_METHOD_NAME)
58+
}
59+
60+
@Test(expected = VerificationException.class)
61+
void verify_void_method_expect_param() {
62+
script.voidMethod()
63+
helper.verify(VOID_METHOD_NAME, 1) { methodCall -> methodCall.args.size() > 0 }
64+
}
65+
66+
@Test(expected = VerificationException.class)
67+
void verify_void_method_less_times() {
68+
script.voidMethod()
69+
script.voidMethod()
70+
helper.verify(VOID_METHOD_NAME)
71+
}
72+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def someStringMethod(String someString1, String someString2="") {
2+
return someString1 + someString2
3+
}
4+
5+
def voidMethod() {
6+
echo "some output"
7+
}

0 commit comments

Comments
 (0)