Skip to content

Commit 84830a1

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Migrate GHMock Rule to Extension * Migrate to DataProviderRunner to ParameterizedTest * Remove public visibility for test classes and methods * Minor clean up
1 parent d2e9e60 commit 84830a1

File tree

53 files changed

+1035
-1080
lines changed

Some content is hidden

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

53 files changed

+1035
-1080
lines changed

pom.xml

+9-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<url>https://repo.jenkins-ci.org/public/</url>
6565
</repository>
6666
</repositories>
67-
67+
6868
<pluginRepositories>
6969
<pluginRepository>
7070
<id>repo.jenkins-ci.org</id>
@@ -135,6 +135,11 @@
135135
<artifactId>mockito-core</artifactId>
136136
<scope>test</scope>
137137
</dependency>
138+
<dependency>
139+
<groupId>org.mockito</groupId>
140+
<artifactId>mockito-junit-jupiter</artifactId>
141+
<scope>test</scope>
142+
</dependency>
138143

139144
<!-- For GlobalMatrixAuthorizationStrategy -->
140145
<dependency>
@@ -172,18 +177,11 @@
172177
<scope>test</scope>
173178
</dependency>
174179

175-
<dependency>
176-
<groupId>com.tngtech.java</groupId>
177-
<artifactId>junit-dataprovider</artifactId>
178-
<version>1.13.1</version>
179-
<scope>test</scope>
180-
</dependency>
181-
182180
<!--to mock github-->
183181
<dependency>
184-
<groupId>com.github.tomakehurst</groupId>
185-
<artifactId>wiremock-jre8-standalone</artifactId>
186-
<version>2.35.2</version>
182+
<groupId>org.wiremock</groupId>
183+
<artifactId>wiremock-standalone</artifactId>
184+
<version>3.10.0</version>
187185
<scope>test</scope>
188186
</dependency>
189187

src/test/java/com/cloudbees/jenkins/GitHubCommitNotifierTest.java

+32-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cloudbees.jenkins;
22

33
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
4-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
4+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
55
import hudson.Launcher;
66
import hudson.model.AbstractBuild;
77
import hudson.model.Build;
@@ -13,24 +13,22 @@
1313
import hudson.plugins.git.Revision;
1414
import hudson.plugins.git.util.BuildData;
1515
import hudson.util.VersionNumber;
16+
import jakarta.inject.Inject;
1617
import org.eclipse.jgit.lib.ObjectId;
1718
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
18-
import org.jenkinsci.plugins.github.test.GHMockRule;
19-
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
20-
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
21-
import org.junit.Before;
22-
import org.junit.Rule;
23-
import org.junit.Test;
24-
import org.junit.rules.RuleChain;
25-
import org.junit.runner.RunWith;
19+
import org.jenkinsci.plugins.github.test.GitHubMockExtension;
20+
import org.jenkinsci.plugins.github.test.GitHubMockExtension.FixedGHRepoNameTestContributor;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.junit.jupiter.api.extension.RegisterExtension;
2625
import org.jvnet.hudson.test.Issue;
2726
import org.jvnet.hudson.test.JenkinsRule;
2827
import org.jvnet.hudson.test.TestBuilder;
2928
import org.jvnet.hudson.test.TestExtension;
29+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3030
import org.mockito.Mock;
31-
import org.mockito.junit.MockitoJUnitRunner;
32-
33-
import jakarta.inject.Inject;
31+
import org.mockito.junit.jupiter.MockitoExtension;
3432

3533
import static com.cloudbees.jenkins.GitHubSetCommitStatusBuilderTest.SOME_SHA;
3634
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
@@ -45,43 +43,42 @@
4543
*
4644
* @author <a href="mailto:[email protected]">Oleg Nenashev</a>
4745
*/
48-
@RunWith(MockitoJUnitRunner.class)
46+
@WithJenkins
47+
@ExtendWith(MockitoExtension.class)
4948
public class GitHubCommitNotifierTest {
5049

51-
@Mock
50+
@Mock(strictness = Mock.Strictness.LENIENT)
5251
public BuildData data;
5352

54-
@Mock
53+
@Mock(strictness = Mock.Strictness.LENIENT)
5554
public Revision rev;
5655

5756
@Inject
5857
public GitHubPluginConfig config;
5958

60-
public JenkinsRule jRule = new JenkinsRule();
59+
private JenkinsRule jRule;
6160

62-
@Rule
63-
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));
64-
65-
@Rule
66-
public GHMockRule github = new GHMockRule(
67-
new WireMockRule(
68-
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
69-
))
61+
@RegisterExtension
62+
static GitHubMockExtension github = new GitHubMockExtension(WireMockExtension.newInstance()
63+
.options(wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))))
7064
.stubUser()
7165
.stubRepo()
7266
.stubStatuses();
7367

7468

75-
@Before
76-
public void before() throws Throwable {
77-
when(data.getLastBuiltRevision()).thenReturn(rev);
78-
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
79-
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
69+
@BeforeEach
70+
void before(JenkinsRule rule) throws Throwable {
71+
jRule = rule;
72+
jRule.getInstance().getInjector().injectMembers(this);
73+
74+
when(data.getLastBuiltRevision()).thenReturn(rev);
75+
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
76+
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
8077
}
8178

8279
@Test
8380
@Issue("JENKINS-23641")
84-
public void testNoBuildData() throws Exception {
81+
void testNoBuildData() throws Exception {
8582
FreeStyleProject prj = jRule.createFreeStyleProject("23641_noBuildData");
8683
prj.getPublishersList().add(new GitHubCommitNotifier());
8784
Build b = prj.scheduleBuild2(0).get();
@@ -91,7 +88,7 @@ public void testNoBuildData() throws Exception {
9188

9289
@Test
9390
@Issue("JENKINS-23641")
94-
public void testNoBuildRevision() throws Exception {
91+
void testNoBuildRevision() throws Exception {
9592
FreeStyleProject prj = jRule.createFreeStyleProject();
9693
prj.setScm(new GitSCM("http://non.existent.git.repo.nowhere/repo.git"));
9794
prj.getPublishersList().add(new GitHubCommitNotifier());
@@ -103,7 +100,7 @@ public void testNoBuildRevision() throws Exception {
103100

104101
@Test
105102
@Issue("JENKINS-25312")
106-
public void testMarkUnstableOnCommitNotifierFailure() throws Exception {
103+
void testMarkUnstableOnCommitNotifierFailure() throws Exception {
107104
FreeStyleProject prj = jRule.createFreeStyleProject();
108105
prj.getPublishersList().add(new GitHubCommitNotifier(Result.UNSTABLE.toString()));
109106
Build b = prj.scheduleBuild2(0).get();
@@ -112,15 +109,15 @@ public void testMarkUnstableOnCommitNotifierFailure() throws Exception {
112109

113110
@Test
114111
@Issue("JENKINS-25312")
115-
public void testMarkSuccessOnCommitNotifierFailure() throws Exception {
112+
void testMarkSuccessOnCommitNotifierFailure() throws Exception {
116113
FreeStyleProject prj = jRule.createFreeStyleProject();
117114
prj.getPublishersList().add(new GitHubCommitNotifier(Result.SUCCESS.toString()));
118115
Build b = prj.scheduleBuild2(0).get();
119116
jRule.assertBuildStatus(Result.SUCCESS, b);
120117
}
121118

122119
@Test
123-
public void shouldWriteStatusOnGH() throws Exception {
120+
void shouldWriteStatusOnGH() throws Exception {
124121
config.getConfigs().add(github.serverConfig());
125122
FreeStyleProject prj = jRule.createFreeStyleProject();
126123

@@ -136,7 +133,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
136133

137134
prj.scheduleBuild2(0).get();
138135

139-
github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
136+
github.verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
140137
}
141138

142139
private Build safelyGenerateBuild(FreeStyleProject prj) throws InterruptedException, java.util.concurrent.ExecutionException {

src/test/java/com/cloudbees/jenkins/GitHubPushTriggerTest.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
import hudson.plugins.git.util.Build;
66
import hudson.plugins.git.util.BuildData;
77
import hudson.util.FormValidation;
8+
import jakarta.inject.Inject;
89
import org.eclipse.jgit.lib.ObjectId;
910
import org.jenkinsci.plugins.github.admin.GitHubHookRegisterProblemMonitor;
1011
import org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventListenerTest;
1112
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
1213
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
1314
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
14-
import org.junit.Before;
15-
import org.junit.Rule;
16-
import org.junit.Test;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
1717
import org.jvnet.hudson.test.Issue;
1818
import org.jvnet.hudson.test.JenkinsRule;
19+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1920

20-
import jakarta.inject.Inject;
2121
import java.io.IOException;
2222
import java.util.HashMap;
2323
import java.util.concurrent.TimeUnit;
@@ -30,7 +30,8 @@
3030
/**
3131
* @author lanwen (Merkushev Kirill)
3232
*/
33-
public class GitHubPushTriggerTest {
33+
@WithJenkins
34+
class GitHubPushTriggerTest {
3435
private static final GitHubRepositoryName REPO = new GitHubRepositoryName("host", "user", "repo");
3536
private static final GitSCM REPO_GIT_SCM = new GitSCM("git://host/user/repo.git");
3637

@@ -40,11 +41,11 @@ public class GitHubPushTriggerTest {
4041
@Inject
4142
private GitHubPushTrigger.DescriptorImpl descriptor;
4243

43-
@Rule
44-
public JenkinsRule jRule = new JenkinsRule();
44+
private JenkinsRule jRule;
4545

46-
@Before
47-
public void setUp() throws Exception {
46+
@BeforeEach
47+
void setUp(JenkinsRule rule) throws Exception {
48+
jRule = rule;
4849
jRule.getInstance().getInjector().injectMembers(this);
4950
}
5051

@@ -53,7 +54,7 @@ public void setUp() throws Exception {
5354
*/
5455
@Test
5556
@Issue("JENKINS-27136")
56-
public void shouldStartWorkflowByTrigger() throws Exception {
57+
void shouldStartWorkflowByTrigger() throws Exception {
5758
WorkflowJob job = jRule.getInstance().createProject(WorkflowJob.class, "test-workflow-job");
5859
GitHubPushTrigger trigger = new GitHubPushTrigger();
5960
trigger.start(job, false);
@@ -79,7 +80,7 @@ public void shouldStartWorkflowByTrigger() throws Exception {
7980

8081
@Test
8182
@Issue("JENKINS-24690")
82-
public void shouldReturnWaringOnHookProblem() throws Exception {
83+
void shouldReturnWaringOnHookProblem() throws Exception {
8384
monitor.registerProblem(REPO, new IOException());
8485
FreeStyleProject job = jRule.createFreeStyleProject();
8586
job.setScm(REPO_GIT_SCM);
@@ -89,7 +90,7 @@ public void shouldReturnWaringOnHookProblem() throws Exception {
8990
}
9091

9192
@Test
92-
public void shouldReturnOkOnNoAnyProblem() throws Exception {
93+
void shouldReturnOkOnNoAnyProblem() throws Exception {
9394
FreeStyleProject job = jRule.createFreeStyleProject();
9495
job.setScm(REPO_GIT_SCM);
9596

src/test/java/com/cloudbees/jenkins/GitHubSetCommitStatusBuilderTest.java

+29-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cloudbees.jenkins;
22

33
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
4-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
4+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
55
import hudson.Launcher;
66
import hudson.model.AbstractBuild;
77
import hudson.model.Build;
@@ -11,26 +11,25 @@
1111
import hudson.plugins.git.Revision;
1212
import hudson.plugins.git.util.BuildData;
1313
import hudson.tasks.Builder;
14+
import jakarta.inject.Inject;
1415
import org.apache.commons.lang3.StringUtils;
1516
import org.eclipse.jgit.lib.ObjectId;
1617
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
17-
import org.jenkinsci.plugins.github.test.GHMockRule;
18-
import org.jenkinsci.plugins.github.test.GHMockRule.FixedGHRepoNameTestContributor;
19-
import org.jenkinsci.plugins.github.test.InjectJenkinsMembersRule;
20-
import org.junit.Before;
21-
import org.junit.Rule;
22-
import org.junit.Test;
23-
import org.junit.rules.RuleChain;
24-
import org.junit.runner.RunWith;
18+
import org.jenkinsci.plugins.github.test.GitHubMockExtension;
19+
import org.jenkinsci.plugins.github.test.GitHubMockExtension.FixedGHRepoNameTestContributor;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.junit.jupiter.api.extension.RegisterExtension;
2524
import org.jvnet.hudson.test.Issue;
2625
import org.jvnet.hudson.test.JenkinsRule;
2726
import org.jvnet.hudson.test.TestBuilder;
2827
import org.jvnet.hudson.test.TestExtension;
28+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2929
import org.jvnet.hudson.test.recipes.LocalData;
3030
import org.mockito.Mock;
31-
import org.mockito.junit.MockitoJUnitRunner;
31+
import org.mockito.junit.jupiter.MockitoExtension;
3232

33-
import jakarta.inject.Inject;
3433
import java.util.List;
3534

3635
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
@@ -44,44 +43,43 @@
4443
*
4544
* @author <a href="mailto:[email protected]">Oleg Nenashev</a>
4645
*/
47-
@RunWith(MockitoJUnitRunner.class)
46+
@WithJenkins
47+
@ExtendWith(MockitoExtension.class)
4848
public class GitHubSetCommitStatusBuilderTest {
4949

5050
public static final String SOME_SHA = StringUtils.repeat("f", 40);
5151

52-
@Mock
52+
@Mock(strictness = Mock.Strictness.LENIENT)
5353
public BuildData data;
5454

55-
@Mock
55+
@Mock(strictness = Mock.Strictness.LENIENT)
5656
public Revision rev;
5757

5858
@Inject
5959
public GitHubPluginConfig config;
6060

61-
public JenkinsRule jRule = new JenkinsRule();
61+
private JenkinsRule jRule;
6262

63-
@Rule
64-
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));
65-
66-
@Rule
67-
public GHMockRule github = new GHMockRule(
68-
new WireMockRule(
69-
wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))
70-
))
63+
@RegisterExtension
64+
static GitHubMockExtension github = new GitHubMockExtension(WireMockExtension.newInstance()
65+
.options(wireMockConfig().dynamicPort().notifier(new Slf4jNotifier(true))))
7166
.stubUser()
7267
.stubRepo()
7368
.stubStatuses();
7469

75-
@Before
76-
public void before() throws Throwable {
77-
when(data.getLastBuiltRevision()).thenReturn(rev);
78-
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
79-
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
70+
@BeforeEach
71+
void before(JenkinsRule rule) throws Throwable {
72+
jRule = rule;
73+
jRule.getInstance().getInjector().injectMembers(this);
74+
75+
when(data.getLastBuiltRevision()).thenReturn(rev);
76+
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
77+
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
8078
}
8179

8280
@Test
8381
@Issue("JENKINS-23641")
84-
public void shouldIgnoreIfNoBuildData() throws Exception {
82+
void shouldIgnoreIfNoBuildData() throws Exception {
8583
FreeStyleProject prj = jRule.createFreeStyleProject("23641_noBuildData");
8684
prj.getBuildersList().add(new GitHubSetCommitStatusBuilder());
8785
Build b = prj.scheduleBuild2(0).get();
@@ -91,7 +89,7 @@ public void shouldIgnoreIfNoBuildData() throws Exception {
9189
@Test
9290
@LocalData
9391
@Issue("JENKINS-32132")
94-
public void shouldLoadNullStatusMessage() throws Exception {
92+
void shouldLoadNullStatusMessage() throws Exception {
9593
config.getConfigs().add(github.serverConfig());
9694
FreeStyleProject prj = jRule.getInstance().getItemByFullName("step", FreeStyleProject.class);
9795

@@ -107,7 +105,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
107105
prj.getBuildersList().replaceBy(builders);
108106
prj.scheduleBuild2(0).get();
109107

110-
github.service().verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
108+
github.verify(1, postRequestedFor(urlPathMatching(".*/" + SOME_SHA)));
111109
}
112110

113111
@TestExtension

0 commit comments

Comments
 (0)