Skip to content

Commit 46cb22f

Browse files
authored
Merge pull request jenkinsci#465 from strangelookingnerd/migrate_to_junit5
Migrate tests to JUnit5
2 parents 56e587f + 88cd1c2 commit 46cb22f

14 files changed

Lines changed: 686 additions & 524 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<dependency>
7878
<groupId>io.jenkins.tools.bom</groupId>
7979
<artifactId>bom-${jenkins.baseline}.x</artifactId>
80-
<version>4023.va_eeb_b_4e45f07</version>
80+
<version>5054.v620b_5d2b_d5e6</version>
8181
<scope>import</scope>
8282
<type>pom</type>
8383
</dependency>

src/test/java/org/jenkinsci/plugins/workflow/BuildQueueTasksTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
package org.jenkinsci.plugins.workflow;
2626

27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.fail;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.fail;
2929

3030
import java.io.IOException;
3131
import java.util.concurrent.ExecutionException;
@@ -34,11 +34,11 @@
3434
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
3535
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
3636
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
37-
import org.junit.Rule;
38-
import org.junit.Test;
37+
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.extension.RegisterExtension;
3939
import org.jvnet.hudson.test.Issue;
4040
import org.jvnet.hudson.test.JenkinsRule;
41-
import org.jvnet.hudson.test.JenkinsSessionRule;
41+
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
4242
import org.xml.sax.SAXException;
4343

4444
import org.htmlunit.Page;
@@ -48,12 +48,14 @@
4848
import net.sf.json.JSONArray;
4949
import net.sf.json.JSONObject;
5050

51-
public class BuildQueueTasksTest {
51+
class BuildQueueTasksTest {
5252

53-
@Rule public JenkinsSessionRule sessions = new JenkinsSessionRule();
53+
@RegisterExtension
54+
private final JenkinsSessionExtension sessions = new JenkinsSessionExtension();
5455

5556
@Issue("JENKINS-28649")
56-
@Test public void queueAPI() throws Throwable {
57+
@Test
58+
void queueAPI() throws Throwable {
5759
// This is implicitly testing ExecutorStepExecution$PlaceholderTask as exported bean
5860
sessions.then(j -> {
5961
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
@@ -66,7 +68,8 @@ public class BuildQueueTasksTest {
6668
}
6769

6870
@Issue("JENKINS-28649")
69-
@Test public void queueAPIRestartable() throws Throwable {
71+
@Test
72+
void queueAPIRestartable() throws Throwable {
7073
// This is implicitly testing AfterRestartTask as exported bean
7174
sessions.then(j -> {
7275
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
@@ -84,15 +87,19 @@ public class BuildQueueTasksTest {
8487
}
8588

8689
@Issue("JENKINS-28649")
87-
@Test public void computerAPI() throws Throwable {
90+
@Test
91+
void computerAPI() throws Throwable {
8892
// This is implicitly testing ExecutorStepExecution$PlaceholderTask$PlaceholderExecutable as exported bean
8993
sessions.then(j -> {
9094
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
9195
p.setDefinition(new CpsFlowDefinition(
92-
"node {\n" +
93-
" echo 'test'\n " +
94-
" semaphore 'watch'\n " +
95-
"}", true));
96+
"""
97+
node {
98+
echo 'test'
99+
100+
semaphore 'watch'
101+
102+
}""", true));
96103

97104
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
98105
SemaphoreStep.waitForStart("watch/1", b);
@@ -128,7 +135,7 @@ private static void assertQueueAPIStatusOKAndAbort(WorkflowRun b, JenkinsRule j)
128135

129136
JSONObject o = JSONObject.fromObject(queue.getWebResponse().getContentAsString());
130137
JSONArray items = o.getJSONArray("items");
131-
// Just check that the request returns HTTP 200 and there is some content.
138+
// Just check that the request returns HTTP 200 and there is some content.
132139
// Not going into de the content in this test
133140
assertEquals(1, items.size());
134141

src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,30 @@
3434
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
3535
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
3636
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
37-
import org.junit.Rule;
38-
import org.junit.Test;
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Test;
3939
import org.jvnet.hudson.test.Issue;
4040
import org.jvnet.hudson.test.JenkinsRule;
41+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
4142

4243
/**
4344
* Verifies that specific environment variables are available.
4445
*
4546
*/
46-
public class EnvWorkflowTest {
47+
@WithJenkins
48+
class EnvWorkflowTest {
4749

48-
@Rule public JenkinsRule r = new JenkinsRule();
50+
private JenkinsRule r;
4951

52+
@BeforeEach
53+
void setUp(JenkinsRule rule) {
54+
r = rule;
55+
}
5056
/**
5157
* Verifies if NODE_NAME environment variable is available on an agent node and on the built-in node.
5258
*/
53-
@Test public void isNodeNameAvailable() throws Exception {
59+
@Test
60+
void isNodeNameAvailable() throws Exception {
5461
r.createSlave("node-test", "unix fast", null);
5562
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "workflow-test");
5663

@@ -109,7 +116,8 @@ private void matchLabelsInAnyOrder(WorkflowRun run, String regex, String... labe
109116
/**
110117
* Verifies if EXECUTOR_NUMBER environment variable is available on an agent node and on the built-in node.
111118
*/
112-
@Test public void isExecutorNumberAvailable() throws Exception {
119+
@Test
120+
void isExecutorNumberAvailable() throws Exception {
113121
r.jenkins.setNumExecutors(1);
114122
r.createSlave("node-test", null, null);
115123
String builtInNodeLabel = r.jenkins.getSelfLabel().getExpression(); // compatibility with 2.307+
@@ -123,15 +131,18 @@ private void matchLabelsInAnyOrder(WorkflowRun run, String regex, String... labe
123131
r.assertLogContains("Executor number on built-in node is 0", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
124132

125133
p.setDefinition(new CpsFlowDefinition(
126-
"node('node-test') {\n" +
127-
" echo \"My number on an agent is ${env.EXECUTOR_NUMBER}\"\n" +
128-
"}\n",
134+
"""
135+
node('node-test') {
136+
echo "My number on an agent is ${env.EXECUTOR_NUMBER}"
137+
}
138+
""",
129139
true));
130140
r.assertLogContains("My number on an agent is 0", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
131141
}
132142

133143
@Issue("JENKINS-33511")
134-
@Test public void isWorkspaceAvailable() throws Exception {
144+
@Test
145+
void isWorkspaceAvailable() throws Exception {
135146
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
136147
String builtInNodeLabel = r.jenkins.getSelfLabel().getExpression(); // compatibility with 2.307+
137148
p.setDefinition(new CpsFlowDefinition("node('" + builtInNodeLabel + "') {echo(/running in ${env.WORKSPACE}/)}", true));

src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/DurableTaskStepTest.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import hudson.slaves.ComputerListener;
3737
import java.io.File;
3838
import java.time.Duration;
39-
import java.util.List;
4039
import java.util.logging.Level;
4140
import java.util.logging.Logger;
4241
import jenkins.slaves.restarter.JnlpSlaveRestarterInstaller;
@@ -45,40 +44,43 @@
4544
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
4645
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4746
import org.jenkinsci.plugins.workflow.log.FileLogStorage;
48-
import org.junit.Rule;
49-
import org.junit.Test;
50-
import org.junit.runner.RunWith;
51-
import org.junit.runners.Parameterized;
47+
import org.junit.jupiter.api.Test;
48+
import org.junit.jupiter.api.extension.RegisterExtension;
49+
import org.junit.jupiter.params.Parameter;
50+
import org.junit.jupiter.params.ParameterizedClass;
51+
import org.junit.jupiter.params.provider.ValueSource;
5252
import org.jvnet.hudson.test.InboundAgentRule;
5353
import org.jvnet.hudson.test.JenkinsRule;
5454
import org.jvnet.hudson.test.PrefixedOutputStream;
55-
import org.jvnet.hudson.test.RealJenkinsRule;
5655
import org.jvnet.hudson.test.TailLog;
56+
import org.jvnet.hudson.test.junit.jupiter.InboundAgentExtension;
57+
import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension;
5758

58-
@RunWith(Parameterized.class)
59-
public final class DurableTaskStepTest {
60-
61-
@Parameterized.Parameters(name = "watching={0}") public static List<Boolean> data() {
62-
return List.of(false, true);
63-
}
64-
65-
@Parameterized.Parameter public boolean useWatching;
59+
@ParameterizedClass(name = "watching={0}")
60+
@ValueSource(booleans = {true, false})
61+
class DurableTaskStepTest {
6662

6763
private static final Logger LOGGER = Logger.getLogger(DurableTaskStepTest.class.getName());
6864

69-
@Rule public RealJenkinsRule rr = new RealJenkinsRule().
65+
@Parameter
66+
private boolean useWatching;
67+
68+
@RegisterExtension
69+
private final RealJenkinsExtension rr = new RealJenkinsExtension().
7070
javaOptions("-Dorg.jenkinsci.plugins.workflow.support.pickles.ExecutorPickle.timeoutForNodeMillis=" + Duration.ofMinutes(5).toMillis()). // reconnection could be >15s esp. on Windows
7171
withColor(PrefixedOutputStream.Color.BLUE).
7272
withLogger(DurableTaskStep.class, Level.FINE).
7373
withLogger(FileMonitoringTask.class, Level.FINE);
7474

75-
@Rule public InboundAgentRule inboundAgents = new InboundAgentRule();
75+
@RegisterExtension
76+
private final InboundAgentExtension inboundAgents = new InboundAgentExtension();
7677

77-
@Test public void scriptExitingAcrossRestart() throws Throwable {
78+
@Test
79+
void scriptExitingAcrossRestart() throws Throwable {
7880
rr.javaOptions("-D" + DurableTaskStep.class.getName() + ".USE_WATCHING=" + useWatching);
7981
rr.startJenkins();
8082
rr.runRemotely(DurableTaskStepTest::disableJnlpSlaveRestarterInstaller);
81-
inboundAgents.createAgent(rr, InboundAgentRule.Options.newBuilder().
83+
inboundAgents.createAgent(rr, InboundAgentExtension.Options.newBuilder().
8284
color(PrefixedOutputStream.Color.MAGENTA).
8385
label("remote").
8486
withLogger(FileMonitoringTask.class, Level.FINER).
@@ -103,7 +105,7 @@ public final class DurableTaskStepTest {
103105
* Most users should be using cloud agents,
104106
* and this lets us preserve {@link InboundAgentRule.Options.Builder#withLogger(Class, Level)}.
105107
*/
106-
private static void disableJnlpSlaveRestarterInstaller(JenkinsRule r) throws Throwable {
108+
private static void disableJnlpSlaveRestarterInstaller(JenkinsRule r) {
107109
ComputerListener.all().remove(ExtensionList.lookupSingleton(JnlpSlaveRestarterInstaller.class));
108110
}
109111

src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/ExitDuringShutdownTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,28 @@
3939
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
4040
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4141
import org.jenkinsci.plugins.workflow.steps.durable_task.exitDuringShutdownTest.FinishProcess;
42-
import static org.junit.Assume.assumeFalse;
43-
import org.junit.Rule;
44-
import org.junit.Test;
42+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
43+
44+
import org.junit.jupiter.api.Test;
45+
import org.junit.jupiter.api.extension.RegisterExtension;
4546
import org.jvnet.hudson.test.PrefixedOutputStream;
46-
import org.jvnet.hudson.test.RealJenkinsRule;
4747
import org.jvnet.hudson.test.TailLog;
48+
import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension;
4849

49-
public final class ExitDuringShutdownTest {
50+
class ExitDuringShutdownTest {
5051

51-
@Rule public RealJenkinsRule rr = new RealJenkinsRule().
52-
addSyntheticPlugin(new RealJenkinsRule.SyntheticPlugin(FinishProcess.class).shortName("ExitDuringShutdownTest").header("Plugin-Dependencies", "workflow-cps:0")).
52+
@RegisterExtension
53+
private final RealJenkinsExtension rr = new RealJenkinsExtension().
54+
addSyntheticPlugin(new RealJenkinsExtension.SyntheticPlugin(FinishProcess.class).shortName("ExitDuringShutdownTest").header("Plugin-Dependencies", "workflow-cps:0")).
5355
javaOptions("-Dorg.jenkinsci.plugins.workflow.support.pickles.ExecutorPickle.timeoutForNodeMillis=" + Duration.ofMinutes(5).toMillis()). // reconnection could be >15s esp. on Windows
5456
javaOptions("-D" + DurableTaskStep.class.getName() + ".USE_WATCHING=true").
5557
withColor(PrefixedOutputStream.Color.BLUE).
5658
withLogger(DurableTaskStep.class, Level.FINE).
5759
withLogger(FileMonitoringTask.class, Level.FINE);
5860

59-
@Test public void scriptExitingDuringShutdown() throws Throwable {
60-
assumeFalse("TODO Windows version TBD", Functions.isWindows());
61+
@Test
62+
void scriptExitingDuringShutdown() throws Throwable {
63+
assumeFalse(Functions.isWindows(), "TODO Windows version TBD");
6164
rr.startJenkins();
6265
try (var tailLog = new TailLog(rr, "p", 1).withColor(PrefixedOutputStream.Color.YELLOW)) {
6366
rr.run(r -> {

src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowerShellStepTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,63 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
24+
2525
package org.jenkinsci.plugins.workflow.steps.durable_task;
2626

27-
import org.junit.Rule;
28-
import org.junit.Test;
29-
import org.junit.Before;
27+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
28+
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
3031
import hudson.model.Result;
3132
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
3233
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
33-
import org.junit.Assume;
3434
import org.jvnet.hudson.test.JenkinsRule;
3535
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
3636
import hudson.Functions;
3737

3838
import java.nio.charset.StandardCharsets;
39+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3940

40-
public class PowerShellStepTest {
41+
@WithJenkins
42+
class PowerShellStepTest {
4143

42-
@Rule public JenkinsRule j = new JenkinsRule();
44+
private JenkinsRule j;
4345

4446
// Ensure that powershell exists and is at least version 3
45-
@Before public void ensurePowerShellSufficientVersion() throws Exception {
47+
@BeforeEach
48+
void setUp(JenkinsRule rule) throws Exception {
49+
j = rule;
4650
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "pretest");
4751
p.setDefinition(new CpsFlowDefinition(Functions.isWindows() ?
4852
"node { bat('powershell.exe -ExecutionPolicy Bypass -NonInteractive -Command \"if ($PSVersionTable.PSVersion.Major -ge 3) {exit 0} else {exit 1}\"') }" :
4953
"node { sh('powershell -NonInteractive -Command \"if ($PSVersionTable.PSVersion.Major -ge 3) {exit 0} else {exit 1}\"') }", true));
5054
WorkflowRun b = p.scheduleBuild2(0).get();
5155
Result r = b.getResult();
52-
Assume.assumeTrue("This test should only run if the pretest workflow job succeeded", r == Result.SUCCESS);
56+
assumeTrue(r == Result.SUCCESS, "This test should only run if the pretest workflow job succeeded");
5357
}
5458

5559
// Test that a powershell step producing particular output indeed has a log containing that output
56-
@Test public void testOutput() throws Exception {
60+
@Test
61+
void testOutput() throws Exception {
5762
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "foo");
5863
p.setDefinition(new CpsFlowDefinition("node {powershell 'Write-Output \"a moon full of stars and astral cars\"'}", true));
5964
j.assertLogContains("a moon full of stars and astral cars", j.assertBuildStatusSuccess(p.scheduleBuild2(0)));
6065
}
61-
66+
6267
// Test that a powershell step that fails indeed causes the underlying build to fail
63-
@Test public void testFailure() throws Exception {
68+
@Test
69+
void testFailure() throws Exception {
6470
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "bar");
6571
p.setDefinition(new CpsFlowDefinition("node {powershell 'throw \"bogus error\"'}", true));
6672
j.assertLogContains("bogus error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0)));
6773
}
68-
69-
@Test public void testUnicode() throws Exception {
74+
75+
@Test
76+
void testUnicode() throws Exception {
7077
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "foobar");
7178
p.setDefinition(new CpsFlowDefinition("node {def x = powershell(returnStdout: true, script: 'write-output \"Hëllö Wórld\"'); println x.replace(\"\ufeff\",\"\")}", true));
7279
String log = new String(JenkinsRule.getLog(j.assertBuildStatusSuccess(p.scheduleBuild2(0))).getBytes(), StandardCharsets.UTF_8);
73-
Assume.assumeTrue("Correct UTF-8 output should be produced",log.contains("Hëllö Wórld"));
80+
assumeTrue(log.contains("Hëllö Wórld"),"Correct UTF-8 output should be produced");
7481
}
7582

7683
}

0 commit comments

Comments
 (0)