Skip to content

Commit 6da0d46

Browse files
Migrate tests to JUnit5 (#389)
* Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor clean up * Bump plugin-pom to 5.7 * Bump plugin pom --------- Co-authored-by: strangelookingnerd <[email protected]>
1 parent 978cb39 commit 6da0d46

19 files changed

+286
-282
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.jenkins-ci.plugins</groupId>
77
<artifactId>plugin</artifactId>
8-
<version>5.4</version>
8+
<version>5.8</version>
99
<relativePath />
1010
</parent>
1111

@@ -62,7 +62,7 @@
6262
<dependency>
6363
<groupId>io.jenkins.tools.bom</groupId>
6464
<artifactId>bom-${jenkins.baseline}.x</artifactId>
65-
<version>3850.vb_c5319efa_e29</version>
65+
<version>4136.vca_c3202a_7fd1</version>
6666
<type>pom</type>
6767
<scope>import</scope>
6868
</dependency>

src/test/java/com/michelin/cio/hudson/plugins/rolestrategy/ApiTest.java

+49-48
Large diffs are not rendered by default.

src/test/java/com/michelin/cio/hudson/plugins/rolestrategy/GrantingDisabledPermissionTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.michelin.cio.hudson.plugins.rolestrategy;
22

3-
import static org.junit.Assert.assertFalse;
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
44

55
import hudson.model.User;
66
import hudson.security.ACL;
@@ -11,17 +11,15 @@
1111
import java.util.HashSet;
1212
import java.util.Map;
1313
import jenkins.model.Jenkins;
14-
import org.junit.Rule;
15-
import org.junit.Test;
14+
import org.junit.jupiter.api.Test;
1615
import org.jvnet.hudson.test.JenkinsRule;
16+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1717

18-
public class GrantingDisabledPermissionTest {
19-
20-
@Rule
21-
public JenkinsRule r = new JenkinsRule();
18+
@WithJenkins
19+
class GrantingDisabledPermissionTest {
2220

2321
@Test
24-
public void grantDisabledPermissionTest() throws Exception {
22+
void grantDisabledPermissionTest(JenkinsRule r) throws Exception {
2523
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(true, false, null);
2624
realm.createAccount("admin", "admin");
2725
realm.createAccount("alice", "alice");

src/test/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@
33
import static org.hamcrest.CoreMatchers.hasItem;
44
import static org.hamcrest.CoreMatchers.not;
55
import static org.hamcrest.MatcherAssert.assertThat;
6-
import static org.junit.Assert.assertFalse;
7-
import static org.junit.Assert.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
88

99
import hudson.security.Permission;
1010
import java.util.Arrays;
1111
import java.util.Collections;
1212
import java.util.HashSet;
1313
import java.util.Set;
14-
import org.junit.Test;
14+
import org.junit.jupiter.api.Test;
1515

16-
public class RoleTest {
16+
class RoleTest {
1717

1818
@Test
19-
public void testHasPermission() {
19+
void testHasPermission() {
2020
Role role = new Role("name", new HashSet<>(Arrays.asList(Permission.CREATE, Permission.READ, Permission.DELETE)));
2121
assertTrue(role.hasPermission(Permission.READ));
2222
assertFalse(role.hasPermission(Permission.WRITE));
2323
}
2424

2525
@Test
26-
public void testHasAnyPermissions() {
26+
void testHasAnyPermissions() {
2727
Role role = new Role("name", new HashSet<>((Arrays.asList(Permission.READ, Permission.DELETE))));
2828
assertTrue(role.hasAnyPermission(new HashSet<>(Arrays.asList(Permission.READ, Permission.WRITE))));
2929
assertFalse(role.hasAnyPermission(new HashSet<>(Arrays.asList(Permission.UPDATE, Permission.WRITE))));
3030
}
3131

3232
@Test
33-
public void shouldNotAddNullPermToNewRole() {
33+
void shouldNotAddNullPermToNewRole() {
3434
Permission permission = Permission.CREATE;
3535
Permission nullPerm = null;
3636

src/test/java/com/synopsys/arc/jenkins/plugins/rolestrategy/ContainedInViewTest.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55

66
import hudson.model.FreeStyleProject;
77
import org.htmlunit.html.HtmlPage;
8-
import org.junit.Before;
9-
import org.junit.Rule;
10-
import org.junit.Test;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
1110
import org.jvnet.hudson.test.JenkinsRule;
1211
import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
1312
import org.jvnet.hudson.test.JenkinsRule.WebClient;
13+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1414
import org.jvnet.hudson.test.recipes.LocalData;
1515

16-
public class ContainedInViewTest {
17-
@Rule
18-
public JenkinsRule jenkinsRule = new JenkinsRule();
16+
@WithJenkins
17+
class ContainedInViewTest {
1918

20-
@Before
19+
private JenkinsRule jenkinsRule;
20+
21+
@BeforeEach
2122
@LocalData
22-
public void setup() throws Exception {
23+
void setup(JenkinsRule jenkinsRule) throws Exception {
24+
this.jenkinsRule = jenkinsRule;
2325
DummySecurityRealm sr = jenkinsRule.createDummySecurityRealm();
2426
jenkinsRule.jenkins.setSecurityRealm(sr);
2527
}
2628

2729
@Test
2830
@LocalData
29-
public void userCanAccessJobInRootView() throws Exception {
31+
void userCanAccessJobInRootView() throws Exception {
3032
FreeStyleProject project = jenkinsRule.jenkins.getItemByFullName("testJob", FreeStyleProject.class);
3133
WebClient wc = jenkinsRule.createWebClient();
3234
wc.withThrowExceptionOnFailingStatusCode(false);
@@ -37,7 +39,7 @@ public void userCanAccessJobInRootView() throws Exception {
3739

3840
@Test
3941
@LocalData
40-
public void userCantAccessJobNotInRootView() throws Exception {
42+
void userCantAccessJobNotInRootView() throws Exception {
4143
FreeStyleProject project = jenkinsRule.jenkins.getItemByFullName("hiddenJob", FreeStyleProject.class);
4244
WebClient wc = jenkinsRule.createWebClient();
4345
wc.withThrowExceptionOnFailingStatusCode(false);
@@ -48,7 +50,7 @@ public void userCantAccessJobNotInRootView() throws Exception {
4850

4951
@Test
5052
@LocalData
51-
public void userCanAccessJobInFolderView() throws Exception {
53+
void userCanAccessJobInFolderView() throws Exception {
5254
FreeStyleProject project = jenkinsRule.jenkins.getItemByFullName("folder/testjob2", FreeStyleProject.class);
5355
WebClient wc = jenkinsRule.createWebClient();
5456
wc.withThrowExceptionOnFailingStatusCode(false);

src/test/java/com/synopsys/arc/jenkins/plugins/rolestrategy/MacroTest.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424

2525
package com.synopsys.arc.jenkins.plugins.rolestrategy;
2626

27-
import org.junit.Assert;
28-
import org.junit.Test;
27+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
28+
import static org.junit.jupiter.api.Assertions.assertFalse;
29+
import static org.junit.jupiter.api.Assertions.fail;
30+
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.Test;
2933

3034
/**
3135
* Contains tests for Macro.
@@ -34,10 +38,10 @@
3438
* @since 2.1.0
3539
* @author Oleg Nenashev
3640
*/
37-
public class MacroTest {
41+
class MacroTest {
3842

3943
@Test
40-
public void correctFormatsSanityCheck() {
44+
void correctFormatsSanityCheck() {
4145
parseMacro("@Dummy");
4246
parseMacro("@Dummy:1");
4347
parseMacro("@Dummy(aaa)");
@@ -47,7 +51,7 @@ public void correctFormatsSanityCheck() {
4751
}
4852

4953
@Test
50-
public void testValidUsers() {
54+
void testValidUsers() {
5155
parseWrongMacro("test", MacroExceptionCode.Not_Macro);
5256
parseWrongMacro("logged_user", MacroExceptionCode.Not_Macro);
5357
parseWrongMacro("anonymous", MacroExceptionCode.Not_Macro);
@@ -56,7 +60,7 @@ public void testValidUsers() {
5660
}
5761

5862
@Test
59-
public void testWrongBrackets() {
63+
void testWrongBrackets() {
6064
parseWrongMacro("@test:1(aaa,bbb(", MacroExceptionCode.WrongFormat);
6165
parseWrongMacro("@test:1(aaa,bbb(", MacroExceptionCode.WrongFormat);
6266
parseWrongMacro("@test:1()(aaa,bbb)", MacroExceptionCode.WrongFormat);
@@ -66,12 +70,12 @@ public void testWrongBrackets() {
6670
}
6771

6872
@Test
69-
public void err_WrongEnding() {
73+
void err_WrongEnding() {
7074
parseWrongMacro("@test:1(aaa,bbb)error", MacroExceptionCode.WrongFormat);
7175
}
7276

7377
@Test
74-
public void err_Quotes() {
78+
void err_Quotes() {
7579
parseWrongMacro("@test':1(aaa,bbb)", MacroExceptionCode.WrongFormat);
7680
parseWrongMacro("@'test':1(aaa,bbb)", MacroExceptionCode.WrongFormat);
7781
parseWrongMacro("@test:1('aaa',bbb)", MacroExceptionCode.WrongFormat);
@@ -85,13 +89,13 @@ public void err_Quotes() {
8589
}
8690

8791
@Test
88-
public void emptyParameters() {
92+
void emptyParameters() {
8993
parseMacro("@Dummy()");
9094
parseMacro("@Dummy:1()");
9195
}
9296

9397
@Test
94-
public void test_MacroSanity() {
98+
void test_MacroSanity() {
9599
testCycle("test1", null, null);
96100
testCycle("test2", 1, null);
97101
testCycle("test3", -1, null);
@@ -114,13 +118,13 @@ private static void testCycle(String macroName, Integer macroId, String[] parame
114118
}
115119

116120
private static void assertEquals(Macro expected, Macro actual) {
117-
Assert.assertEquals("Wrong name", expected.getName(), expected.getName());
118-
Assert.assertEquals("Wrong index", expected.getIndex(), expected.getIndex());
121+
Assertions.assertEquals(expected.getName(), actual.getName(), "Wrong name");
122+
Assertions.assertEquals(expected.getIndex(), actual.getIndex(), "Wrong index");
119123

120124
if (expected.hasParameters()) {
121-
Assert.assertArrayEquals("Wrong parameters set", expected.getParameters(), actual.getParameters());
125+
assertArrayEquals(expected.getParameters(), actual.getParameters(), "Wrong parameters set");
122126
} else {
123-
Assert.assertFalse("Actual macro shouldn't have parameters", actual.hasParameters());
127+
assertFalse(actual.hasParameters(), "Actual macro shouldn't have parameters");
124128
}
125129
}
126130

@@ -132,15 +136,15 @@ private static Macro assertParseMacroDriver(String errorMessage, String macroStr
132136
} catch (MacroException ex) {
133137
ex.printStackTrace();
134138
if (expectException) {
135-
Assert.assertEquals(errorMessage + ". Wrong error code", expectedError, ex.getErrorCode());
139+
Assertions.assertEquals(expectedError, ex.getErrorCode(), errorMessage + ". Wrong error code");
136140
} else {
137-
Assert.fail(errorMessage + ". Got Macro Exception: " + ex.getMessage());
141+
fail(errorMessage + ". Got Macro Exception: " + ex.getMessage());
138142
}
139143
return res;
140144
}
141145

142146
if (expectException) {
143-
Assert.fail(errorMessage + ". Haven't got exception. Expected code is " + expectedError);
147+
fail(errorMessage + ". Haven't got exception. Expected code is " + expectedError);
144148
}
145149
return res;
146150
}

src/test/java/jmh/BenchmarkRunner.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import java.util.concurrent.TimeUnit;
44
import jenkins.benchmark.jmh.BenchmarkFinder;
5-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
66
import org.openjdk.jmh.annotations.Mode;
77
import org.openjdk.jmh.results.format.ResultFormatType;
88
import org.openjdk.jmh.runner.Runner;
99
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
1010
import org.openjdk.jmh.runner.options.OptionsBuilder;
1111

12-
public final class BenchmarkRunner {
12+
class BenchmarkRunner {
1313
@Test
14-
public void runJmhBenchmarks() throws Exception {
14+
void runJmhBenchmarks() throws Exception {
1515
ChainedOptionsBuilder options = new OptionsBuilder()
1616
.mode(Mode.AverageTime)
1717
.warmupIterations(2)

src/test/java/org/jenkinsci/plugins/rolestrategy/AuthorizeProjectTest.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,26 @@
2929
import org.jenkinsci.plugins.authorizeproject.AuthorizeProjectProperty;
3030
import org.jenkinsci.plugins.authorizeproject.ProjectQueueItemAuthenticator;
3131
import org.jenkinsci.plugins.authorizeproject.strategy.TriggeringUsersAuthorizationStrategy;
32-
import org.junit.Before;
33-
import org.junit.Rule;
34-
import org.junit.Test;
32+
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.Test;
3534
import org.jvnet.hudson.test.JenkinsRule;
3635
import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
36+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3737
import org.jvnet.hudson.test.recipes.LocalData;
3838

39-
public class AuthorizeProjectTest {
39+
@WithJenkins
40+
class AuthorizeProjectTest {
4041

41-
@Rule
42-
public JenkinsRule jenkinsRule = new JenkinsRule();
42+
private JenkinsRule jenkinsRule;
4343

4444
private Slave node;
4545
private FreeStyleProject project;
4646
private AuthorizationCheckBuilder checker;
4747

48-
@Before
48+
@BeforeEach
4949
@LocalData
50-
public void setup() throws Exception {
50+
void setup(JenkinsRule jenkinsRule) throws Exception {
51+
this.jenkinsRule = jenkinsRule;
5152
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(Collections.emptyMap()));
5253
node = jenkinsRule.createSlave("TestAgent", null, null);
5354
jenkinsRule.waitOnline(node);
@@ -62,7 +63,7 @@ public void setup() throws Exception {
6263

6364
@Test
6465
@LocalData
65-
public void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception {
66+
void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception {
6667
try (ACLContext c = ACL.as(User.getById("tester", true))) {
6768
project.scheduleBuild2(0, new Cause.UserIdCause());
6869
}
@@ -75,7 +76,7 @@ public void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception {
7576

7677
@Test
7778
@LocalData
78-
public void missingAgentBuildPermissionsBlockBuild() throws Exception {
79+
void missingAgentBuildPermissionsBlockBuild() throws Exception {
7980
try (ACLContext c = ACL.as(User.getById("reader", true))) {
8081
project.scheduleBuild2(0, new Cause.UserIdCause());
8182
}
@@ -102,7 +103,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
102103
}
103104

104105
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
105-
@SuppressWarnings("rawtypes")
106+
106107
@Override
107108
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
108109
return true;

0 commit comments

Comments
 (0)