forked from jenkinsci/JenkinsPipelineUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jenkinsci#463 from nestoracunablanco/feature/verif…
…yMethod Adds verify method in BasePipelineTest.
- Loading branch information
Showing
5 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/groovy/com/lesfurets/jenkins/unit/VerificationException.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
72
src/test/groovy/com/lesfurets/jenkins/unit/VerifyTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |