Skip to content

Commit c695588

Browse files
Migrate tests to JUnit Jupiter
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 760d758 commit c695588

File tree

46 files changed

+375
-349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+375
-349
lines changed

build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ dependencies {
2929
api('org.assertj:assertj-core:3.27.6')
3030
implementation('org.apache.commons:commons-lang3:3.19.0')
3131

32-
testImplementation('junit:junit:4.13.2')
32+
testImplementation('org.junit.jupiter:junit-jupiter:6.0.1')
33+
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:6.0.1')
34+
testRuntimeOnly('org.junit.platform:junit-platform-launcher:6.0.1')
3335
}
3436

3537
defaultTasks 'test'
@@ -45,6 +47,7 @@ tasks.register('sourcesJar', Jar) {
4547
}
4648

4749
test {
50+
useJUnitPlatform()
4851

4952
// Enable writing stacks with -Ppipeline.stack.write
5053
if (project.hasProperty("pipeline.stack.write")) {

src/test/groovy/com/TestCatchError.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.BasePipelineTest
4-
import org.junit.Before
5-
import org.junit.Test
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
66

77
class TestCatchError extends BasePipelineTest {
88

99
@Override
10-
@Before
10+
@BeforeEach
1111
void setUp() throws Exception {
1212
super.setUp()
1313
}
1414

15-
@Test()
15+
@Test
1616
void should_fail_with_fail_before_and_SuccesCatch() throws Exception {
1717
def script = runInlineScript("""
1818
node() {
@@ -27,7 +27,7 @@ class TestCatchError extends BasePipelineTest {
2727
assertJobStatusFailure()
2828
}
2929

30-
@Test()
30+
@Test
3131
void should_unstable_with_unstable_before_and_SuccesCatch() throws Exception {
3232
def script = runInlineScript("""
3333
node() {
@@ -42,7 +42,7 @@ class TestCatchError extends BasePipelineTest {
4242
assertJobStatusUnstable()
4343
}
4444

45-
@Test()
45+
@Test
4646
void should_succes_with_SuccesCatch() throws Exception {
4747
def script = runInlineScript("""
4848
node() {
@@ -56,7 +56,7 @@ class TestCatchError extends BasePipelineTest {
5656
assertJobStatusSuccess()
5757
}
5858

59-
@Test()
59+
@Test
6060
void should_unstable_with_UnstableCatch() throws Exception {
6161
def script = runInlineScript("""
6262
node() {
@@ -70,7 +70,7 @@ class TestCatchError extends BasePipelineTest {
7070
assertJobStatusUnstable()
7171
}
7272

73-
@Test()
73+
@Test
7474
void should_fail_with_no_parameter() throws Exception {
7575
def script = runInlineScript("""
7676
node() {

src/test/groovy/com/lesfurets/jenkins/TestAddCredential.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.BasePipelineTest
4-
import org.junit.Test
4+
import org.junit.jupiter.api.Test
55

66
class TestAddCredential extends BasePipelineTest {
77

src/test/groovy/com/lesfurets/jenkins/TestAddEnvVar.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.BasePipelineTest
4-
import org.junit.Test
4+
import org.junit.jupiter.api.Test
55

66
class TestAddEnvVar extends BasePipelineTest {
77

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.BasePipelineTest
4-
import org.junit.Test
4+
import org.junit.jupiter.api.Test
5+
6+
import static org.junit.jupiter.api.Assertions.assertThrows
57

68
class TestAddParam extends BasePipelineTest {
79

@@ -11,20 +13,24 @@ class TestAddParam extends BasePipelineTest {
1113
addParam('FOO', 'BAR')
1214
}
1315

14-
@Test(expected = UnsupportedOperationException)
16+
@Test
1517
void addParamImmutable() throws Exception {
1618
super.setUp()
1719
addParam('FOO', 'BAR')
1820

19-
// We should not be able to modify existing parameters. This would not work on Jenkins.
20-
binding.getVariable('params')['FOO'] = 'NOT-BAR'
21+
assertThrows(UnsupportedOperationException, { ->
22+
// We should not be able to modify existing parameters. This would not work on Jenkins.
23+
binding.getVariable('params')['FOO'] = 'NOT-BAR'
24+
})
2125
}
2226

23-
@Test(expected = UnsupportedOperationException)
27+
@Test
2428
void addNewParamImmutable() throws Exception {
2529
super.setUp()
2630

27-
// It also is not permitted to add new parameters directly. Instead, addParam must be used.
28-
binding.getVariable('params')['BAZ'] = 'QUX'
31+
assertThrows(UnsupportedOperationException, { ->
32+
// It also is not permitted to add new parameters directly. Instead, addParam must be used.
33+
binding.getVariable('params')['BAZ'] = 'QUX'
34+
})
2935
}
3036
}

src/test/groovy/com/lesfurets/jenkins/TestCustomMethodJob.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.BasePipelineTest
4-
import org.junit.Before
5-
import org.junit.Test
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
66

7-
import static org.junit.Assert.assertTrue
7+
import static org.junit.jupiter.api.Assertions.assertTrue
88

99
class TestCustomMethodJob extends BasePipelineTest {
1010

1111
@Override
12-
@Before
12+
@BeforeEach
1313
void setUp() throws Exception {
1414
scriptRoots += 'src/test/jenkins'
1515
super.setUp()

src/test/groovy/com/lesfurets/jenkins/TestCustomMethodJobCPS.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS
4-
import org.junit.Before
5-
import org.junit.Test
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
66

7-
import static org.junit.Assert.assertTrue
7+
import static org.junit.jupiter.api.Assertions.assertTrue
88

99
class TestCustomMethodJobCPS extends BasePipelineTestCPS {
1010

1111
@Override
12-
@Before
12+
@BeforeEach
1313
void setUp() throws Exception {
1414
scriptRoots += 'src/test/jenkins'
1515
super.setUp()
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package com.lesfurets.jenkins
22

33
import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest
4-
import org.junit.Before
5-
import org.junit.Test
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
66

7-
import static org.junit.Assert.assertEquals
7+
import static org.junit.jupiter.api.Assertions.assertEquals
8+
import static org.junit.jupiter.api.Assertions.assertThrows
89

910
class TestDeclarativeImmutableParams extends DeclarativePipelineTest {
1011

1112
@Override
12-
@Before
13+
@BeforeEach
1314
void setUp() throws Exception {
1415
scriptRoots += 'src/test/jenkins'
1516

1617
super.setUp()
1718
}
1819

19-
@Test(expected = UnsupportedOperationException)
20+
@Test
2021
void "test immutable params in declarative pipeline"() {
21-
runScript("job/library/test_params_immutable_declarative.jenkins")
22-
assertEquals('original', binding.params['new'].toString())
22+
assertThrows(UnsupportedOperationException, { ->
23+
runScript("job/library/test_params_immutable_declarative.jenkins")
24+
})
25+
assertEquals('null', binding.params['new'].toString())
2326
}
2427
}

src/test/groovy/com/lesfurets/jenkins/TestExampleJob.groovy

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.lesfurets.jenkins
22

3-
import org.junit.Before
4-
import org.junit.Test
5-
63
import com.lesfurets.jenkins.unit.BasePipelineTest
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
76

87
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
9-
import static org.junit.Assert.assertTrue
8+
import static org.junit.jupiter.api.Assertions.assertTrue
109

1110
class TestExampleJob extends BasePipelineTest {
1211

1312
@Override
14-
@Before
13+
@BeforeEach
1514
void setUp() throws Exception {
1615
scriptRoots += 'src/test/jenkins'
1716
super.setUp()

src/test/groovy/com/lesfurets/jenkins/TestExampleJobCPS.groovy

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.lesfurets.jenkins
22

3-
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
4-
import static org.junit.Assert.assertTrue
5-
6-
import org.junit.Before
7-
import org.junit.Test
8-
93
import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Test
6+
7+
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
8+
import static org.junit.jupiter.api.Assertions.assertTrue
109

1110
class TestExampleJobCPS extends BasePipelineTestCPS {
1211

1312
@Override
14-
@Before
13+
@BeforeEach
1514
void setUp() throws Exception {
1615
scriptRoots += 'src/test/jenkins'
1716
super.setUp()

0 commit comments

Comments
 (0)