Skip to content

Commit 04e39cd

Browse files
strangelookingnerdslide
authored andcommitted
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent c82e698 commit 04e39cd

28 files changed

+860
-921
lines changed

src/test/java/org/jenkinsci/plugins/tokenmacro/DataBoundTokenMacroTest.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,36 @@
66

77
package org.jenkinsci.plugins.tokenmacro;
88

9-
import static org.junit.Assert.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
1011

1112
import hudson.model.AbstractBuild;
1213
import hudson.model.FreeStyleBuild;
1314
import hudson.model.FreeStyleProject;
1415
import hudson.model.TaskListener;
15-
import java.io.IOException;
16-
import org.junit.Rule;
17-
import org.junit.Test;
16+
17+
import org.junit.jupiter.api.Test;
1818
import org.jvnet.hudson.test.JenkinsRule;
1919
import org.jvnet.hudson.test.TestExtension;
20+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2021

2122
/**
2223
*
2324
* @author acearl
2425
*/
25-
public class DataBoundTokenMacroTest {
26-
27-
@Rule
28-
public final JenkinsRule j = new JenkinsRule();
26+
@WithJenkins
27+
class DataBoundTokenMacroTest {
2928

3029
@Test
31-
public void testSimpleDataBoundMacro() throws Exception {
30+
void testSimpleDataBoundMacro(JenkinsRule j) throws Exception {
3231
FreeStyleProject p = j.createFreeStyleProject("foo");
3332
FreeStyleBuild b = p.scheduleBuild2(0).get();
3433

3534
assertEquals("foo", TokenMacro.expand(b, TaskListener.NULL, "${TEST_MACRO, arg=\"foo\"}"));
3635
}
3736

3837
@Test
39-
public void testMethodDataBoundMacro() throws Exception {
38+
void testMethodDataBoundMacro(JenkinsRule j) throws Exception {
4039
FreeStyleProject p = j.createFreeStyleProject("foo");
4140
FreeStyleBuild b = p.scheduleBuild2(0).get();
4241

@@ -45,16 +44,16 @@ public void testMethodDataBoundMacro() throws Exception {
4544
assertEquals("YES", TokenMacro.expand(b, TaskListener.NULL, "${ENUM_MACRO, value=\"YES\"}"));
4645
}
4746

48-
@Test(expected = MacroEvaluationException.class)
49-
public void testMethodDataBoundMacroThrows() throws Exception {
47+
@Test
48+
void testMethodDataBoundMacroThrows(JenkinsRule j) throws Exception {
5049
FreeStyleProject p = j.createFreeStyleProject("foo");
5150
FreeStyleBuild b = p.scheduleBuild2(0).get();
52-
53-
TokenMacro.expand(b, TaskListener.NULL, "${ENUM_MACRO, value=\"BAD\"}");
51+
assertThrows(MacroEvaluationException.class, () ->
52+
TokenMacro.expand(b, TaskListener.NULL, "${ENUM_MACRO, value=\"BAD\"}"));
5453
}
5554

5655
@Test
57-
public void testDataBoundMacroWithFieldAlias() throws Exception {
56+
void testDataBoundMacroWithFieldAlias(JenkinsRule j) throws Exception {
5857
FreeStyleProject p = j.createFreeStyleProject("foo");
5958
FreeStyleBuild b = p.scheduleBuild2(0).get();
6059

@@ -63,7 +62,7 @@ public void testDataBoundMacroWithFieldAlias() throws Exception {
6362
}
6463

6564
@Test
66-
public void testDataBoundMacroWithMethodAlias() throws Exception {
65+
void testDataBoundMacroWithMethodAlias(JenkinsRule j) throws Exception {
6766
FreeStyleProject p = j.createFreeStyleProject("foo");
6867
FreeStyleBuild b = p.scheduleBuild2(0).get();
6968

@@ -73,7 +72,7 @@ public void testDataBoundMacroWithMethodAlias() throws Exception {
7372
}
7473

7574
@Test
76-
public void testRecursionLimit() throws Exception {
75+
void testRecursionLimit(JenkinsRule j) throws Exception {
7776
FreeStyleProject p = j.createFreeStyleProject("foo");
7877
FreeStyleBuild b = p.scheduleBuild2(0).get();
7978

@@ -87,8 +86,7 @@ public static class SimpleDataBoundMacro extends DataBoundTokenMacro {
8786
public String arg = "default";
8887

8988
@Override
90-
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName)
91-
throws MacroEvaluationException, IOException, InterruptedException {
89+
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) {
9290
return arg;
9391
}
9492

@@ -115,8 +113,7 @@ public void setValue(final String value) {
115113
}
116114

117115
@Override
118-
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName)
119-
throws MacroEvaluationException, IOException, InterruptedException {
116+
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) {
120117
return this.value.toString();
121118
}
122119

@@ -140,8 +137,7 @@ public void setArg2(int arg2) {
140137
}
141138

142139
@Override
143-
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName)
144-
throws MacroEvaluationException, IOException, InterruptedException {
140+
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) {
145141
return String.format("default = %s, arg2 = %d", arg, arg2);
146142
}
147143

@@ -154,8 +150,7 @@ public boolean acceptsMacroName(String macroName) {
154150
@TestExtension
155151
public static class RecursiveDataBoundMacro extends DataBoundTokenMacro {
156152
@Override
157-
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName)
158-
throws MacroEvaluationException, IOException, InterruptedException {
153+
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) {
159154
int level = Integer.parseInt(macroName.substring(9));
160155
if (level > 10) {
161156
return "DONE!";

src/test/java/org/jenkinsci/plugins/tokenmacro/PipelineTest.java

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.jenkinsci.plugins.tokenmacro;
22

3-
import static junit.framework.TestCase.assertEquals;
4-
import static org.junit.Assert.fail;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.fail;
56

67
import com.google.common.collect.ListMultimap;
78
import hudson.FilePath;
@@ -23,62 +24,63 @@
2324
import jenkins.security.MasterToSlaveCallable;
2425
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
2526
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
26-
import org.junit.Before;
27-
import org.junit.Rule;
28-
import org.junit.Test;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
2929
import org.jvnet.hudson.test.JenkinsRule;
3030
import org.jvnet.hudson.test.TestExtension;
31+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3132

3233
/**
3334
* Created by acearl on 6/14/2016.
3435
*/
35-
public class PipelineTest {
36+
@WithJenkins
37+
class PipelineTest {
3638
private StreamTaskListener listener;
3739

38-
@Rule
39-
public final JenkinsRule j = new JenkinsRule();
40+
private JenkinsRule j;
4041

4142
private DumbSlave agent;
4243

43-
@Before
44-
public void setup() throws Exception {
44+
@BeforeEach
45+
void setup(JenkinsRule j) throws Exception {
46+
this.j = j;
4547
agent = j.createOnlineSlave(Label.get("agents"));
4648
}
4749

4850
@Test
49-
public void testEnvironmentVariables() throws Exception {
51+
void testEnvironmentVariables() throws Exception {
5052
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
5153
job.setDefinition(new CpsFlowDefinition(getPipeline("any", "${ENV, var=\"VERSION\"}"), true));
5254
Run<?, ?> run = j.assertBuildStatusSuccess(job.scheduleBuild2(0));
5355
j.assertLogContains("VERSION=1.0.0", run);
5456
}
5557

5658
@Test
57-
public void testEnvironmentVariablesNoAgent() throws Exception {
59+
void testEnvironmentVariablesNoAgent() throws Exception {
5860
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
5961
job.setDefinition(new CpsFlowDefinition(getPipeline("none", "${ENV, var=\"VERSION\"}"), true));
6062
Run<?, ?> run = j.assertBuildStatusSuccess(job.scheduleBuild2(0));
6163
j.assertLogContains("VERSION=1.0.0", run);
6264
}
6365

6466
@Test
65-
public void testWorkspaceNeededNoAgent() throws Exception {
67+
void testWorkspaceNeededNoAgent() throws Exception {
6668
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
6769
job.setDefinition(new CpsFlowDefinition(getPipeline("none", "${TEST_WS}"), true));
6870
Run<?, ?> run = j.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0));
6971
j.assertLogContains("Macro 'TEST_WS' can ony be evaluated in a workspace", run);
7072
}
7173

7274
@Test
73-
public void testWorkspaceNeededWithAgent() throws Exception {
75+
void testWorkspaceNeededWithAgent() throws Exception {
7476
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
7577
job.setDefinition(new CpsFlowDefinition(getPipeline("any", "${TEST_WS}"), true));
7678
Run<?, ?> run = j.assertBuildStatusSuccess(job.scheduleBuild2(0));
7779
j.assertLogContains("Workspace: foo", run);
7880
}
7981

8082
@Test
81-
public void testFileNeededWithAgent() throws Exception {
83+
void testFileNeededWithAgent() throws Exception {
8284
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
8385
FilePath workspace = agent.getWorkspaceFor(job);
8486
workspace.mkdirs();
@@ -91,7 +93,7 @@ public void testFileNeededWithAgent() throws Exception {
9193
}
9294

9395
@Test
94-
public void testEscapedExpandAll() throws Exception {
96+
void testEscapedExpandAll() throws Exception {
9597
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
9698

9799
job.setDefinition(new CpsFlowDefinition("node('agents') {\n\techo 'Hello, world'\n}", true));
@@ -101,35 +103,26 @@ public void testEscapedExpandAll() throws Exception {
101103
assertEquals(j.jenkins.getRootUrl() + "job/foo/1/", TokenMacro.expand(run, null, listener, "${BUILD_URL}"));
102104
assertEquals(j.jenkins.getRootUrl() + "job/foo/1/", TokenMacro.expand(run, null, listener, "$BUILD_URL"));
103105

104-
assertEquals(
105-
"{abc=[def, ghi], jkl=[true]}",
106-
TokenMacro.expand(run, null, listener, "${TEST,abc=\"def\",abc=\"ghi\",jkl=true}"));
106+
assertEquals("{abc=[def, ghi], jkl=[true]}", TokenMacro.expand(run, null, listener, "${TEST,abc=\"def\",abc=\"ghi\",jkl=true}"));
107107
}
108108

109109
@Test
110-
public void testException() throws Exception {
110+
void testException() throws Exception {
111111
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
112112
job.setDefinition(new CpsFlowDefinition("node('agents') {\n\techo 'Hello, world'\n}", true));
113113
Run<?, ?> run = j.assertBuildStatusSuccess(job.scheduleBuild2(0));
114114

115115
listener = StreamTaskListener.fromStdout();
116116

117-
try {
118-
TokenMacro.expand(run, null, listener, "${TEST_NESTEDX}");
119-
fail();
120-
} catch (MacroEvaluationException e) {
121-
// do nothing, just want to catch the exception when it occurs
122-
}
117+
assertThrows(MacroEvaluationException.class, () -> TokenMacro.expand(run, null, listener, "${TEST_NESTEDX}"));
123118

124119
assertEquals(" ${TEST_NESTEDX}", TokenMacro.expand(run, null, listener, " ${TEST_NESTEDX}", false, null));
125-
assertEquals(
126-
"${TEST_NESTEDX,abc=\"def\",abc=\"ghi\",jkl=true}",
127-
TokenMacro.expand(
128-
run, null, listener, "${TEST_NESTEDX,abc=\"def\",abc=\"ghi\",jkl=true}", false, null));
120+
assertEquals("${TEST_NESTEDX,abc=\"def\",abc=\"ghi\",jkl=true}", TokenMacro.expand(
121+
run, null, listener, "${TEST_NESTEDX,abc=\"def\",abc=\"ghi\",jkl=true}", false, null));
129122
}
130123

131124
@Test
132-
public void testUnconvertedMacro() throws Exception {
125+
void testUnconvertedMacro() throws Exception {
133126
WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "foo");
134127
job.setDefinition(new CpsFlowDefinition("node('agents') {\n\techo 'Hello, world'\n}", true));
135128
Run<?, ?> run = j.assertBuildStatusSuccess(job.scheduleBuild2(0));
@@ -174,8 +167,7 @@ public String evaluate(
174167
TaskListener listener,
175168
String macroName,
176169
Map<String, String> arguments,
177-
ListMultimap<String, String> argumentMultimap)
178-
throws MacroEvaluationException, IOException, InterruptedException {
170+
ListMultimap<String, String> argumentMultimap) {
179171
return evaluate(context, null, listener, macroName, arguments, argumentMultimap);
180172
}
181173

@@ -186,8 +178,7 @@ public String evaluate(
186178
TaskListener listener,
187179
String macroName,
188180
Map<String, String> arguments,
189-
ListMultimap<String, String> argumentMultimap)
190-
throws MacroEvaluationException, IOException, InterruptedException {
181+
ListMultimap<String, String> argumentMultimap) {
191182
return argumentMultimap.toString();
192183
}
193184
}
@@ -207,8 +198,7 @@ public String evaluate(
207198
TaskListener listener,
208199
String macroName,
209200
Map<String, String> arguments,
210-
ListMultimap<String, String> argumentMultimap)
211-
throws MacroEvaluationException, IOException, InterruptedException {
201+
ListMultimap<String, String> argumentMultimap) {
212202
return "${TEST,abc=\"def\",abc=\"ghi\",jkl=true}";
213203
}
214204

@@ -243,8 +233,7 @@ public String evaluate(
243233
TaskListener listener,
244234
String macroName,
245235
Map<String, String> arguments,
246-
ListMultimap<String, String> argumentMultimap)
247-
throws MacroEvaluationException, IOException, InterruptedException {
236+
ListMultimap<String, String> argumentMultimap) {
248237
return argumentMultimap.toString();
249238
}
250239
}
@@ -276,11 +265,11 @@ public String evaluate(
276265

277266
@Override
278267
public Callable<String, IOException> getCallable(Run<?, ?> run, String root, TaskListener listener) {
279-
return new MasterToSlaveCallable<String, IOException>() {
280-
@Override
281-
public String call() throws IOException {
282-
return "Workspace: " + new File(root).getName();
283-
}
268+
return new MasterToSlaveCallable<>() {
269+
@Override
270+
public String call() {
271+
return "Workspace: " + new File(root).getName();
272+
}
284273
};
285274
}
286275
}

src/test/java/org/jenkinsci/plugins/tokenmacro/TokenMacroStepTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
44
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
5-
import org.junit.Rule;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
76
import org.jvnet.hudson.test.JenkinsRule;
7+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
88

99
/**
1010
* Created by acearl on 4/10/2017.
1111
*/
12-
public class TokenMacroStepTest {
13-
@Rule
14-
public JenkinsRule j = new JenkinsRule();
12+
@WithJenkins
13+
class TokenMacroStepTest {
1514

1615
@Test
17-
public void basics() throws Exception {
16+
void basics(JenkinsRule j) throws Exception {
1817
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
1918
p.setDefinition(new CpsFlowDefinition("node { def val = tm('${BUILD_NUMBER}') ; echo val }", true));
2019
j.assertLogContains("1", j.assertBuildStatusSuccess(p.scheduleBuild2(0)));

0 commit comments

Comments
 (0)