Skip to content

Commit f725ecf

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 664bdaf commit f725ecf

File tree

8 files changed

+247
-146
lines changed

8 files changed

+247
-146
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<parent>
2929
<groupId>org.jenkins-ci.plugins</groupId>
3030
<artifactId>plugin</artifactId>
31-
<version>5.24</version>
31+
<version>5.26</version>
3232
<relativePath />
3333
</parent>
3434
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -68,6 +68,7 @@
6868
<jenkins.baseline>2.479</jenkins.baseline>
6969
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
7070
<no-test-jar>false</no-test-jar>
71+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
7172
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
7273
</properties>
7374
<dependencyManagement>
@@ -82,6 +83,12 @@
8283
</dependencies>
8384
</dependencyManagement>
8485
<dependencies>
86+
<dependency>
87+
<groupId>org.awaitility</groupId>
88+
<artifactId>awaitility</artifactId>
89+
<version>4.3.0</version>
90+
<scope>test</scope>
91+
</dependency>
8592
<dependency>
8693
<groupId>org.mockito</groupId>
8794
<artifactId>mockito-core</artifactId>

src/test/java/org/jenkinsci/plugins/workflow/steps/AbstractStepImplTest.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import hudson.Extension;
44
import hudson.model.Node;
55
import jenkins.model.Jenkins;
6-
import static org.junit.Assert.*;
7-
import org.junit.Before;
8-
import org.junit.Rule;
9-
import org.junit.Test;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
1010
import org.jvnet.hudson.test.JenkinsRule;
11+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1112
import org.kohsuke.stapler.DataBoundConstructor;
1213
import org.kohsuke.stapler.DataBoundSetter;
1314

@@ -21,32 +22,33 @@
2122
* @author Kohsuke Kawaguchi
2223
*/
2324
@SuppressWarnings("deprecation") // it is all deprecated
24-
public class AbstractStepImplTest {
25-
@Rule
26-
public JenkinsRule j = new JenkinsRule();
25+
@WithJenkins
26+
class AbstractStepImplTest {
27+
28+
private JenkinsRule j;
2729

2830
@Inject
29-
BogusStep.DescriptorImpl d;
31+
private BogusStep.DescriptorImpl d;
3032

31-
@Before
32-
public void setUp() {
33+
@BeforeEach
34+
void setUp(JenkinsRule rule) {
35+
j = rule;
3336
j.getInstance().getInjector().injectMembers(this);
3437
}
3538

3639
@Test
37-
public void inject() throws Exception {
38-
40+
void inject() throws Exception {
3941
Map<String, Object> r = new HashMap<>();
4042
r.put("a",3);
4143
r.put("b","bbb");
4244
r.put("c",null);
4345
r.put("d","ddd");
4446
BogusStep step = (BogusStep) d.newInstance(r);
4547

46-
assertEquals(step.a,3);
47-
assertEquals(step.b,"bbb");
48+
assertEquals(3, step.a);
49+
assertEquals("bbb", step.b);
4850
assertNull(step.c);
49-
assertEquals(step.d,"ddd");
51+
assertEquals("ddd", step.d);
5052

5153
StepContext c = mock(StepContext.class);
5254
when(c.get(Node.class)).thenReturn(j.getInstance());
@@ -56,10 +58,12 @@ public void inject() throws Exception {
5658
}
5759

5860
public static class BogusStep extends AbstractStepImpl {
59-
int a;
60-
String b;
61-
@DataBoundSetter String c;
62-
Object d;
61+
62+
private int a;
63+
private String b;
64+
@DataBoundSetter
65+
private String c;
66+
private Object d;
6367

6468
@DataBoundConstructor
6569
public BogusStep(int a, String b) {
@@ -92,11 +96,12 @@ public String getDisplayName() {
9296
}
9397

9498
public static class BogusStepExecution extends AbstractSynchronousStepExecution<Void> {
99+
95100
@Inject
96-
Jenkins jenkins;
101+
private Jenkins jenkins;
97102

98103
@StepContextParameter
99-
Node n;
104+
private Node n;
100105

101106
@Override
102107
protected Void run() {

src/test/java/org/jenkinsci/plugins/workflow/steps/DynamicContextTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
package org.jenkinsci.plugins.workflow.steps;
2626

2727
import java.io.IOException;
28-
import org.junit.Test;
29-
import static org.junit.Assert.*;
28+
import org.junit.jupiter.api.Test;
3029

31-
public class DynamicContextTest {
30+
import static org.junit.jupiter.api.Assertions.assertNull;
31+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3232

33-
@Test public void subclassing() throws Exception {
33+
class DynamicContextTest {
34+
35+
@Test
36+
void subclassing() throws Exception {
3437
class Super {}
3538
class Sub extends Super {}
3639
class Sub2 extends Super {}
@@ -48,10 +51,9 @@ class Dyn extends DynamicContext.Typed<Super> {
4851
}
4952
}
5053
DynamicContext ctx = new Dyn();
51-
assertNotNull("can look up via supertype", ctx.get(Super.class, nullContext));
52-
assertNotNull("can look up via subtype", ctx.get(Sub.class, nullContext));
53-
assertNull("but not via a mismatched subtype", ctx.get(Sub2.class, nullContext));
54-
assertNull("nor via an unrelated supertype", ctx.get(Runnable.class, nullContext));
54+
assertNotNull(ctx.get(Super.class, nullContext), "can look up via supertype");
55+
assertNotNull(ctx.get(Sub.class, nullContext), "can look up via subtype");
56+
assertNull(ctx.get(Sub2.class, nullContext), "but not via a mismatched subtype");
57+
assertNull(ctx.get(Runnable.class, nullContext), "nor via an unrelated supertype");
5558
}
56-
5759
}

src/test/java/org/jenkinsci/plugins/workflow/steps/GeneralNonBlockingStepExecutionTest.java

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424

2525
package org.jenkinsci.plugins.workflow.steps;
2626

27+
import static org.awaitility.Awaitility.await;
2728
import static org.hamcrest.MatcherAssert.assertThat;
2829
import static org.hamcrest.Matchers.containsString;
2930
import static org.hamcrest.Matchers.empty;
30-
import static org.junit.Assert.assertTrue;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
3132

3233
import hudson.model.Result;
3334
import hudson.model.TaskListener;
@@ -41,26 +42,41 @@
4142
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4243
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
4344
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
44-
import org.junit.Before;
45-
import org.junit.ClassRule;
46-
import org.junit.Rule;
47-
import org.junit.Test;
48-
import org.jvnet.hudson.test.BuildWatcher;
45+
import org.junit.jupiter.api.BeforeEach;
46+
import org.junit.jupiter.api.Test;
47+
import org.junit.jupiter.api.extension.RegisterExtension;
4948
import org.jvnet.hudson.test.Issue;
5049
import org.jvnet.hudson.test.JenkinsRule;
51-
import org.jvnet.hudson.test.LoggerRule;
50+
import org.jvnet.hudson.test.LogRecorder;
5251
import org.jvnet.hudson.test.TestExtension;
52+
import org.jvnet.hudson.test.junit.jupiter.BuildWatcherExtension;
53+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
5354
import org.kohsuke.stapler.DataBoundConstructor;
5455

55-
public class GeneralNonBlockingStepExecutionTest {
56+
@WithJenkins
57+
class GeneralNonBlockingStepExecutionTest {
5658

57-
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
59+
@SuppressWarnings("unused")
60+
@RegisterExtension
61+
private static final BuildWatcherExtension BUILD_WATCHER = new BuildWatcherExtension();
5862

59-
@Rule public JenkinsRule r = new JenkinsRule();
63+
private static Semaphore startEnter, startExit, endEnter, endExit;
64+
65+
private final LogRecorder logging = new LogRecorder();
6066

61-
@Rule public LoggerRule logging = new LoggerRule();
67+
private JenkinsRule r;
68+
69+
@BeforeEach
70+
void setUp(JenkinsRule rule) {
71+
r = rule;
72+
startEnter = new Semaphore(0);
73+
startExit = new Semaphore(0);
74+
endEnter = new Semaphore(0);
75+
endExit = new Semaphore(0);
76+
}
6277

63-
@Test public void getStatus() throws Exception {
78+
@Test
79+
void getStatus() throws Exception {
6480
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
6581
p.setDefinition(new CpsFlowDefinition("slowBlock {semaphore 'wait'}", true));
6682
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
@@ -71,9 +87,7 @@ public class GeneralNonBlockingStepExecutionTest {
7187
startExit.release();
7288
SemaphoreStep.waitForStart("wait/1", b);
7389
assertThat(((CpsFlowExecution) b.getExecution()).getThreadDump().toString(), containsString("at DSL.slowBlock(not currently scheduled, or running blocks)"));
74-
while (b.getExecutor().getAsynchronousExecution().blocksRestart()) {
75-
Thread.sleep(100); // as above
76-
}
90+
await().until(() -> !b.getExecutor().getAsynchronousExecution().blocksRestart());
7791
SemaphoreStep.success("wait/1", null);
7892
endEnter.acquire();
7993
assertThat(((CpsFlowExecution) b.getExecution()).getThreadDump().toString(), containsString("at DSL.slowBlock(running in thread: org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution [#"));
@@ -83,7 +97,8 @@ public class GeneralNonBlockingStepExecutionTest {
8397
r.assertBuildStatusSuccess(r.waitForCompletion(b));
8498
}
8599

86-
@Test public void stop() throws Exception {
100+
@Test
101+
void stop() throws Exception {
87102
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
88103
p.setDefinition(new CpsFlowDefinition("slowBlock {semaphore 'wait'}", true));
89104
logging.record(CpsStepContext.class, Level.WARNING).capture(100);
@@ -120,7 +135,8 @@ public class GeneralNonBlockingStepExecutionTest {
120135
}
121136

122137
@Issue("JENKINS-58878")
123-
@Test public void shouldNotHang() throws Exception {
138+
@Test
139+
void shouldNotHang() throws Exception {
124140
int iterations = 50;
125141
startExit.release(iterations); // Prevents the semaphores from blocking inside of the slowBlock step.
126142
endExit.release(iterations);
@@ -134,61 +150,72 @@ public class GeneralNonBlockingStepExecutionTest {
134150
r.buildAndAssertSuccess(p);
135151
}
136152

137-
private static Semaphore startEnter, startExit, endEnter, endExit;
153+
@SuppressWarnings("unused")
154+
public static final class SlowBlockStep extends Step {
138155

139-
@Before public void semaphores() {
140-
startEnter = new Semaphore(0);
141-
startExit = new Semaphore(0);
142-
endEnter = new Semaphore(0);
143-
endExit = new Semaphore(0);
144-
}
156+
@DataBoundConstructor
157+
public SlowBlockStep() {}
145158

146-
public static final class SlowBlockStep extends Step {
147-
@DataBoundConstructor public SlowBlockStep() {}
148-
@Override public StepExecution start(StepContext context) {
159+
@Override
160+
public StepExecution start(StepContext context) {
149161
return new Execution(context, this);
150162
}
163+
151164
private static final class Execution extends GeneralNonBlockingStepExecution {
152165
private final transient SlowBlockStep step;
166+
153167
Execution(StepContext context, SlowBlockStep step) {
154168
super(context);
155169
this.step = step;
156170
}
171+
157172
private void println(String msg) throws Exception {
158173
getContext().get(TaskListener.class).getLogger().println(msg);
159174
}
160-
@Override public boolean start() throws Exception {
175+
176+
@Override
177+
public boolean start() throws Exception {
161178
println("starting step");
162179
run(this::doStart);
163180
return false;
164181
}
182+
165183
private void doStart() throws Exception {
166184
println("starting background part of step");
167185
startEnter.release();
168186
startExit.acquire();
169187
println("starting body");
170188
getContext().newBodyInvoker().withCallback(new Callback()).start();
171189
}
190+
172191
private final class Callback extends TailCall {
173-
@Override protected void finished(StepContext context) throws Exception {
192+
193+
@Override
194+
protected void finished(StepContext context) throws Exception {
174195
println("body completed, starting background end part of step");
175196
endEnter.release();
176197
endExit.acquire();
177198
println("ending step");
178199
}
179200
}
180201
}
181-
@TestExtension public static final class DescriptorImpl extends StepDescriptor {
182-
@Override public String getFunctionName() {
202+
@TestExtension
203+
public static final class DescriptorImpl extends StepDescriptor {
204+
205+
@Override
206+
public String getFunctionName() {
183207
return "slowBlock";
184208
}
185-
@Override public Set<? extends Class<?>> getRequiredContext() {
209+
210+
@Override
211+
public Set<? extends Class<?>> getRequiredContext() {
186212
return Collections.singleton(TaskListener.class);
187213
}
188-
@Override public boolean takesImplicitBlockArgument() {
214+
215+
@Override
216+
public boolean takesImplicitBlockArgument() {
189217
return true;
190218
}
191219
}
192220
}
193-
194221
}

src/test/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableExceptionTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,25 @@
2727
import hudson.model.Result;
2828
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
2929
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
30-
import org.junit.Rule;
31-
import org.junit.Test;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
3232
import org.jvnet.hudson.test.JenkinsRule;
33+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3334

34-
public final class MissingContextVariableExceptionTest {
35+
@WithJenkins
36+
class MissingContextVariableExceptionTest {
3537

36-
@Rule public JenkinsRule r = new JenkinsRule();
38+
private JenkinsRule r;
3739

38-
@Test public void message() throws Exception {
40+
@BeforeEach
41+
void setUp(JenkinsRule rule) {
42+
r = rule;
43+
}
44+
45+
@Test
46+
void message() throws Exception {
3947
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
4048
p.setDefinition(new CpsFlowDefinition("sh 'oops'", true));
4149
r.assertLogContains("Perhaps you forgot to surround the sh step with a step that provides this, such as: node", r.buildAndAssertStatus(Result.FAILURE, p));
4250
}
43-
4451
}

0 commit comments

Comments
 (0)