Skip to content

Commit f0a8d2d

Browse files
Migrate tests to JUnit Jupiter (#1825)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup * Ban JUnit4 imports Co-authored-by: Kris Stern <krisstern@outlook.com>
1 parent da20165 commit f0a8d2d

File tree

9 files changed

+102
-119
lines changed

9 files changed

+102
-119
lines changed

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
5656
<mockserver.version>5.15.0</mockserver.version>
5757
<spotless.check.skip>false</spotless.check.skip>
58+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
5859
</properties>
5960

6061
<dependencyManagement>
@@ -263,13 +264,6 @@
263264
<version>${mockserver.version}</version>
264265
<scope>test</scope>
265266
</dependency>
266-
<!-- used by UpdateGitLabCommitStatusStepTest -->
267-
<dependency>
268-
<groupId>org.mock-server</groupId>
269-
<artifactId>mockserver-junit-rule</artifactId>
270-
<version>${mockserver.version}</version>
271-
<scope>test</scope>
272-
</dependency>
273267
<dependency>
274268
<groupId>org.mock-server</groupId>
275269
<artifactId>mockserver-netty</artifactId>

src/test/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisherTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,23 @@ void matrixAggregatable() throws Exception {
9696
}
9797

9898
@Test
99-
void running_v3() throws Exception {
99+
void running_v3() {
100100
AbstractBuild build = mockBuild(GITLAB_CONNECTION_V3, null, "test/project.git");
101101
HttpRequest[] requests = prepareCheckCommitAndUpdateStatusRequests("v3", build, BuildState.running);
102102

103103
prebuildAndVerify(build, listener, requests);
104104
}
105105

106106
@Test
107-
void running_v4() throws Exception {
107+
void running_v4() {
108108
AbstractBuild build = mockBuild(GITLAB_CONNECTION_V4, null, "test/project.git");
109109
HttpRequest[] requests = prepareCheckCommitAndUpdateStatusRequests("v4", build, BuildState.running);
110110

111111
prebuildAndVerify(build, listener, requests);
112112
}
113113

114114
@Test
115-
void runningWithLibrary() throws Exception {
115+
void runningWithLibrary() {
116116
AbstractBuild build = mockBuildWithLibrary(GITLAB_CONNECTION_V4, null, "test/project.git");
117117
HttpRequest[] requests = prepareCheckCommitAndUpdateStatusRequests("v4", build, BuildState.running);
118118

@@ -228,7 +228,7 @@ void unstableAsSuccess() throws Exception {
228228
}
229229

230230
@Test
231-
void running_multipleRepos() throws Exception {
231+
void running_multipleRepos() {
232232
AbstractBuild build = mockBuild(GITLAB_CONNECTION_V4, null, "test/project-1.git", "test/project-2.git");
233233
HttpRequest[] requests = new HttpRequest[] {
234234
prepareExistsCommitWithSuccessResponse("v4", "test/project-1"),
@@ -241,7 +241,7 @@ void running_multipleRepos() throws Exception {
241241
}
242242

243243
@Test
244-
void running_commitNotExists() throws Exception {
244+
void running_commitNotExists() {
245245
AbstractBuild build = mockBuild(GITLAB_CONNECTION_V4, null, "test/project.git");
246246
HttpRequest updateCommitStatus =
247247
prepareUpdateCommitStatusWithSuccessResponse("v4", "test/project", build, BuildState.running);
@@ -251,7 +251,7 @@ void running_commitNotExists() throws Exception {
251251
}
252252

253253
@Test
254-
void running_failToUpdate() throws Exception {
254+
void running_failToUpdate() {
255255
AbstractBuild build = mockBuild(GITLAB_CONNECTION_V4, null, "test/project.git");
256256
BuildListener buildListener = mock(BuildListener.class);
257257

src/test/java/com/dabsquared/gitlabjenkins/trigger/filter/AllBranchesFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AllBranchesFilterTest {
1313

1414
@Test
1515
void isRandomBranchNameAllowed() {
16-
String randomBranchName = RandomStringUtils.random(10, true, false);
16+
String randomBranchName = RandomStringUtils.secure().next(10, true, false);
1717

1818
assertThat(new AllBranchesFilter().isBranchAllowed(null, randomBranchName), is(true));
1919
}

src/test/java/com/dabsquared/gitlabjenkins/trigger/handler/push/PushHookTriggerHandlerGitlabServerTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ void createRevisionParameterAction_pushNewTagRequest(GitLabPushRequestSamples sa
101101
@ParameterizedTest
102102
@MethodSource("data")
103103
void doNotCreateRevisionParameterAction_deleteBranchRequest(GitLabPushRequestSamples samples) {
104-
assertThrows(NoRevisionToBuildException.class, () -> {
105-
PushHook hook = samples.deleteBranchRequest();
106-
new PushHookTriggerHandlerImpl(false).createRevisionParameter(hook, null);
107-
});
104+
PushHook hook = samples.deleteBranchRequest();
105+
assertThrows(NoRevisionToBuildException.class, () -> new PushHookTriggerHandlerImpl(false)
106+
.createRevisionParameter(hook, null));
108107
}
109108

110109
@ParameterizedTest

src/test/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookActionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void testNullNotifierTokenAllowsAccess() {
8787
assertTrue(action.performOnPostCalled, "performOnPost not called, token did not match?");
8888
}
8989

90-
public class BuildWebHookActionImpl extends BuildWebHookAction {
90+
public static class BuildWebHookActionImpl extends BuildWebHookAction {
9191

9292
// Used for the assertion that tokenMatches() returned true
9393
public boolean performOnPostCalled = false;

src/test/java/com/dabsquared/gitlabjenkins/webhook/build/NoteBuildActionTest.java

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -69,69 +69,69 @@ void setUp() throws Exception {
6969
}
7070

7171
@Test
72-
void build() {
73-
assertThrows(HttpResponses.HttpResponseException.class, () -> {
74-
FreeStyleProject testProject = jenkins.createFreeStyleProject();
75-
testProject.addTrigger(trigger);
76-
new NoteBuildAction(testProject, getJson("NoteEvent.json"), null).execute(response);
77-
78-
verify(trigger).onPost(any(NoteHook.class));
79-
});
72+
void build() throws Exception {
73+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
74+
testProject.addTrigger(trigger);
75+
76+
assertThrows(
77+
HttpResponses.HttpResponseException.class,
78+
() -> new NoteBuildAction(testProject, getJson("NoteEvent.json"), null).execute(response));
79+
verify(trigger).onPost(any(NoteHook.class));
8080
}
8181

8282
@Test
83-
void build_alreadyBuiltMR_alreadyBuiltMR() {
84-
assertThrows(HttpResponses.HttpResponseException.class, () -> {
85-
FreeStyleProject testProject = jenkins.createFreeStyleProject();
86-
testProject.addTrigger(trigger);
87-
testProject.setScm(new GitSCM(gitRepoUrl));
88-
QueueTaskFuture<?> future = testProject.scheduleBuild2(
89-
0, new ParametersAction(new StringParameterValue("gitlabTargetBranch", "master")));
90-
future.get();
91-
new NoteBuildAction(testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null).execute(response);
92-
93-
verify(trigger).onPost(any(NoteHook.class));
94-
});
83+
void build_alreadyBuiltMR_alreadyBuiltMR() throws Exception {
84+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
85+
testProject.addTrigger(trigger);
86+
testProject.setScm(new GitSCM(gitRepoUrl));
87+
QueueTaskFuture<?> future = testProject.scheduleBuild2(
88+
0, new ParametersAction(new StringParameterValue("gitlabTargetBranch", "master")));
89+
future.get();
90+
91+
assertThrows(HttpResponses.HttpResponseException.class, () -> new NoteBuildAction(
92+
testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null)
93+
.execute(response));
94+
verify(trigger).onPost(any(NoteHook.class));
9595
}
9696

9797
@Test
98-
void build_alreadyBuiltMR_differentTargetBranch() {
99-
assertThrows(HttpResponses.HttpResponseException.class, () -> {
100-
FreeStyleProject testProject = jenkins.createFreeStyleProject();
101-
testProject.addTrigger(trigger);
102-
testProject.setScm(new GitSCM(gitRepoUrl));
103-
QueueTaskFuture<?> future = testProject.scheduleBuild2(
104-
0,
105-
new GitLabWebHookCause(causeData()
106-
.withActionType(CauseData.ActionType.NOTE)
107-
.withSourceProjectId(1)
108-
.withTargetProjectId(1)
109-
.withBranch("feature")
110-
.withSourceBranch("feature")
111-
.withUserName("")
112-
.withSourceRepoHomepage("https://gitlab.org/test")
113-
.withSourceRepoName("test")
114-
.withSourceNamespace("test-namespace")
115-
.withSourceRepoUrl("git@gitlab.org:test.git")
116-
.withSourceRepoSshUrl("git@gitlab.org:test.git")
117-
.withSourceRepoHttpUrl("https://gitlab.org/test.git")
118-
.withMergeRequestTitle("Test")
119-
.withMergeRequestId(1)
120-
.withMergeRequestIid(1)
121-
.withTargetBranch("master")
122-
.withTargetRepoName("test")
123-
.withTargetNamespace("test-namespace")
124-
.withTargetRepoSshUrl("git@gitlab.org:test.git")
125-
.withTargetRepoHttpUrl("https://gitlab.org/test.git")
126-
.withTriggeredByUser("test")
127-
.withLastCommit("123")
128-
.withTargetProjectUrl("https://gitlab.org/test")
129-
.build()));
130-
future.get();
131-
new NoteBuildAction(testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null).execute(response);
132-
133-
verify(trigger).onPost(any(NoteHook.class));
134-
});
98+
void build_alreadyBuiltMR_differentTargetBranch() throws Exception {
99+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
100+
testProject.addTrigger(trigger);
101+
testProject.setScm(new GitSCM(gitRepoUrl));
102+
QueueTaskFuture<?> future = testProject.scheduleBuild2(
103+
0,
104+
new GitLabWebHookCause(causeData()
105+
.withActionType(CauseData.ActionType.NOTE)
106+
.withSourceProjectId(1)
107+
.withTargetProjectId(1)
108+
.withBranch("feature")
109+
.withSourceBranch("feature")
110+
.withUserName("")
111+
.withSourceRepoHomepage("https://gitlab.org/test")
112+
.withSourceRepoName("test")
113+
.withSourceNamespace("test-namespace")
114+
.withSourceRepoUrl("git@gitlab.org:test.git")
115+
.withSourceRepoSshUrl("git@gitlab.org:test.git")
116+
.withSourceRepoHttpUrl("https://gitlab.org/test.git")
117+
.withMergeRequestTitle("Test")
118+
.withMergeRequestId(1)
119+
.withMergeRequestIid(1)
120+
.withTargetBranch("master")
121+
.withTargetRepoName("test")
122+
.withTargetNamespace("test-namespace")
123+
.withTargetRepoSshUrl("git@gitlab.org:test.git")
124+
.withTargetRepoHttpUrl("https://gitlab.org/test.git")
125+
.withTriggeredByUser("test")
126+
.withLastCommit("123")
127+
.withTargetProjectUrl("https://gitlab.org/test")
128+
.build()));
129+
future.get();
130+
131+
assertThrows(HttpResponses.HttpResponseException.class, () -> new NoteBuildAction(
132+
testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null)
133+
.execute(response));
134+
verify(trigger).onPost(any(NoteHook.class));
135135
}
136136

137137
private String getJson(String name) throws Exception {

src/test/java/com/dabsquared/gitlabjenkins/webhook/build/PipelineBuildActionTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.jupiter.api.Assertions.assertThrows;
44
import static org.mockito.ArgumentMatchers.any;
5-
import static org.mockito.Mockito.never;
65
import static org.mockito.Mockito.verify;
76

87
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
@@ -51,20 +50,18 @@ void setUp() throws Exception {
5150

5251
@Test
5352
void buildOnSuccess() {
54-
assertThrows(HttpResponses.HttpResponseException.class, () -> {
55-
new PipelineBuildAction(testProject, getJson("PipelineEvent.json"), null).execute(response);
56-
57-
verify(trigger).onPost(any(PipelineHook.class));
58-
});
53+
assertThrows(
54+
HttpResponses.HttpResponseException.class,
55+
() -> new PipelineBuildAction(testProject, getJson("PipelineEvent.json"), null).execute(response));
56+
verify(trigger).onPost(any(PipelineHook.class));
5957
}
6058

6159
@Test
6260
void doNotBuildOnFailure() {
63-
assertThrows(HttpResponses.HttpResponseException.class, () -> {
64-
new PipelineBuildAction(testProject, getJson("PipelineFailureEvent.json"), null).execute(response);
65-
66-
verify(trigger, never()).onPost(any(PipelineHook.class));
67-
});
61+
assertThrows(HttpResponses.HttpResponseException.class, () -> new PipelineBuildAction(
62+
testProject, getJson("PipelineFailureEvent.json"), null)
63+
.execute(response));
64+
verify(trigger).onPost(any(PipelineHook.class));
6865
}
6966

7067
private String getJson(String name) throws Exception {

src/test/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildActionTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ void build() {
8989
}
9090

9191
@Test
92-
void invalidToken() {
93-
assertThrows(HttpResponses.HttpResponseException.class, () -> {
94-
FreeStyleProject testProject = jenkins.createFreeStyleProject();
95-
when(trigger.getTriggerOpenMergeRequestOnPush()).thenReturn(TriggerOpenMergeRequest.never);
96-
when(trigger.getSecretToken()).thenReturn("secret");
97-
testProject.addTrigger(trigger);
98-
new PushBuildAction(testProject, getJson("PushEvent.json"), "wrong-secret").execute(response);
92+
void invalidToken() throws Exception {
93+
FreeStyleProject testProject = jenkins.createFreeStyleProject();
94+
when(trigger.getTriggerOpenMergeRequestOnPush()).thenReturn(TriggerOpenMergeRequest.never);
95+
when(trigger.getSecretToken()).thenReturn("secret");
96+
testProject.addTrigger(trigger);
9997

100-
verify(trigger, never()).onPost(any(PushHook.class));
101-
});
98+
assertThrows(
99+
HttpResponses.HttpResponseException.class,
100+
() -> new PushBuildAction(testProject, getJson("PushEvent.json"), "wrong-secret").execute(response));
101+
verify(trigger, never()).onPost(any(PushHook.class));
102102
}
103103

104104
private String getJson(String name) throws Exception {

src/test/java/com/dabsquared/gitlabjenkins/workflow/UpdateGitLabCommitStatusStepTest.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,34 @@
1616
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
1717
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
1818
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
19-
import org.junit.After;
20-
import org.junit.Before;
21-
import org.junit.Rule;
22-
import org.junit.Test;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
2323
import org.jvnet.hudson.test.JenkinsRule;
24-
import org.jvnet.hudson.test.RealJenkinsRule;
24+
import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension;
2525
import org.mockserver.client.MockServerClient;
26-
import org.mockserver.junit.MockServerRule;
26+
import org.mockserver.junit.jupiter.MockServerExtension;
2727

28-
public class UpdateGitLabCommitStatusStepTest {
28+
@ExtendWith(MockServerExtension.class)
29+
class UpdateGitLabCommitStatusStepTest {
2930

30-
@Rule
31-
public RealJenkinsRule rr = new RealJenkinsRule();
32-
33-
@Rule
34-
public MockServerRule mockServer = new MockServerRule(new Object());
31+
@RegisterExtension
32+
private final RealJenkinsExtension extension = new RealJenkinsExtension();
3533

3634
private MockServerClient mockServerClient;
3735

38-
@Before
39-
public void setUp() {
40-
mockServerClient = new MockServerClient("localhost", mockServer.getPort());
41-
}
42-
43-
@After
44-
public void tearDown() {
45-
mockServerClient.reset();
36+
@BeforeEach
37+
void setUp(MockServerClient client) {
38+
mockServerClient = client;
4639
}
4740

4841
@Test
49-
public void updateGitlabCommitStatus() throws Throwable {
50-
int port = mockServer.getPort();
42+
void updateGitlabCommitStatus() throws Throwable {
43+
int port = mockServerClient.getPort();
5144
String pipelineText = IOUtils.toString(
5245
getClass().getResourceAsStream("pipeline/updateGitlabCommitStatus.groovy"), StandardCharsets.UTF_8);
53-
rr.then(j -> {
46+
extension.then(j -> {
5447
_updateGitlabCommitStatus(j, port, pipelineText);
5548
});
5649
}

0 commit comments

Comments
 (0)