Skip to content

Commit 92f3823

Browse files
strangelookingnerdMarkEWaitekrisstern
authored
Migrate tests to JUnit5 (test / jenkins / 3) (jenkinsci#10581)
* Migrate tests to JUnit5 (test / jenkins / 3) * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup * Stabilize SetupWizardTest --------- Co-authored-by: Mark Waite <mark.earl.waite@gmail.com> Co-authored-by: Kris Stern <krisstern@outlook.com>
1 parent d653b08 commit 92f3823

File tree

119 files changed

+2495
-1910
lines changed

Some content is hidden

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

119 files changed

+2495
-1910
lines changed

test/src/test/java/jenkins/ClassPathTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@
3333
import java.util.TreeMap;
3434
import java.util.jar.JarEntry;
3535
import java.util.jar.JarFile;
36-
import org.junit.Rule;
37-
import org.junit.Test;
38-
import org.junit.rules.ErrorCollector;
36+
import org.junit.jupiter.api.AfterEach;
37+
import org.junit.jupiter.api.Test;
3938
import org.jvnet.hudson.test.Issue;
4039
import org.jvnet.hudson.test.WarExploder;
40+
import org.opentest4j.MultipleFailuresError;
4141

42-
public class ClassPathTest {
42+
class ClassPathTest {
4343

44-
@Rule
45-
public ErrorCollector errors = new ErrorCollector();
44+
private final List<Throwable> errors = new ArrayList<>();
4645

4746
private static final Set<String> KNOWN_VIOLATIONS = Set.of(
4847
// TODO duplicated in [jline-2.14.6.jar, jansi-1.11.jar]
@@ -75,9 +74,16 @@ public class ClassPathTest {
7574
"org/fusesource/jansi/internal/WindowsSupport.class",
7675
"org/fusesource/jansi/WindowsAnsiOutputStream.class");
7776

77+
@AfterEach
78+
void tearDown() {
79+
if (!errors.isEmpty()) {
80+
throw new MultipleFailuresError(null, errors);
81+
}
82+
}
83+
7884
@Issue("JENKINS-46754")
7985
@Test
80-
public void uniqueness() throws Exception {
86+
void uniqueness() throws Exception {
8187
Map<String, List<String>> entries = new TreeMap<>();
8288
for (File jar : new File(WarExploder.getExplodedDir(), "WEB-INF/lib").listFiles((dir, name) -> name.endsWith(".jar"))) {
8389
String jarname = jar.getName();
@@ -93,7 +99,7 @@ public void uniqueness() throws Exception {
9399
}
94100
entries.forEach((name, jarnames) -> {
95101
if (jarnames.size() > 1 && !KNOWN_VIOLATIONS.contains(name)) { // Matchers.hasSize unfortunately does not display the collection
96-
errors.addError(new AssertionError(name + " duplicated in " + jarnames));
102+
errors.add(new AssertionError(name + " duplicated in " + jarnames));
97103
}
98104
});
99105
}

test/src/test/java/jenkins/CoreAutomaticTestBuilder.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/src/test/java/jenkins/ExtensionFilterTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.hasSize;
5-
import static org.junit.Assert.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import hudson.ExtensionComponent;
88
import hudson.console.ConsoleAnnotatorFactory;
99
import hudson.model.PageDecorator;
1010
import jenkins.install.SetupWizard;
11-
import org.junit.Rule;
12-
import org.junit.Test;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
1313
import org.jvnet.hudson.test.JenkinsRule;
1414
import org.jvnet.hudson.test.TestExtension;
15+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1516

1617
/**
1718
* @author Kohsuke Kawaguchi
1819
*/
19-
public class ExtensionFilterTest {
20+
@WithJenkins
21+
class ExtensionFilterTest {
2022

21-
@Rule
22-
public JenkinsRule j = new JenkinsRule();
23+
private JenkinsRule j;
24+
25+
@BeforeEach
26+
void setUp(JenkinsRule rule) {
27+
j = rule;
28+
}
2329

2430
@Test
25-
public void filter() {
31+
void filter() {
2632
assertThat(PageDecorator.all(), hasSize(1));
2733
assertTrue(ConsoleAnnotatorFactory.all().isEmpty());
2834
}

test/src/test/java/jenkins/I18nTest.java

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,88 +26,97 @@
2626

2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.startsWith;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
2930

3031
import java.io.IOException;
3132
import net.sf.json.JSONObject;
32-
import org.junit.Assert;
33-
import org.junit.Rule;
34-
import org.junit.Test;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
3535
import org.jvnet.hudson.test.Issue;
3636
import org.jvnet.hudson.test.JenkinsRule;
37-
import org.xml.sax.SAXException;
37+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3838

3939
/**
4040
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
4141
*/
42-
public class I18nTest {
42+
@WithJenkins
43+
class I18nTest {
4344

44-
@Rule
45-
public JenkinsRule jenkinsRule = new JenkinsRule();
45+
private JenkinsRule jenkinsRule;
46+
47+
@BeforeEach
48+
void setUp(JenkinsRule rule) {
49+
jenkinsRule = rule;
50+
}
4651

4752
@Test
48-
public void test_baseName_unspecified() throws IOException, SAXException {
53+
void test_baseName_unspecified() throws IOException {
4954
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle").getJSONObject();
50-
Assert.assertEquals("error", response.getString("status"));
51-
Assert.assertEquals("Mandatory parameter 'baseName' not specified.", response.getString("message"));
55+
assertEquals("error", response.getString("status"));
56+
assertEquals("Mandatory parameter 'baseName' not specified.", response.getString("message"));
5257
}
5358

5459
@Test
55-
public void test_baseName_unknown() throws IOException, SAXException {
60+
void test_baseName_unknown() throws IOException {
5661
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=com.acme.XyzWhatever").getJSONObject();
57-
Assert.assertEquals("error", response.getString("status"));
62+
assertEquals("error", response.getString("status"));
5863
assertThat(response.getString("message"), startsWith("Can't find bundle for base name com.acme.XyzWhatever"));
5964
}
6065

6166
@Issue("JENKINS-35270")
6267
@Test
63-
public void test_baseName_plugin() throws Exception {
68+
void test_baseName_plugin() throws Exception {
6469
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=org.jenkinsci.plugins.matrixauth.Messages").getJSONObject();
65-
Assert.assertEquals(response.toString(), "ok", response.getString("status"));
70+
assertEquals("ok", response.getString("status"), response.toString());
6671
JSONObject data = response.getJSONObject("data");
67-
Assert.assertEquals("Matrix-based security", data.getString("GlobalMatrixAuthorizationStrategy.DisplayName"));
72+
assertEquals("Matrix-based security", data.getString("GlobalMatrixAuthorizationStrategy.DisplayName"));
6873
}
6974

7075
@Test
71-
public void test_valid() throws IOException, SAXException {
76+
void test_valid() throws IOException {
7277
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=hudson.logging.Messages&language=de").getJSONObject();
73-
Assert.assertEquals("ok", response.getString("status"));
78+
assertEquals("ok", response.getString("status"));
7479
JSONObject data = response.getJSONObject("data");
75-
Assert.assertEquals("Initialisiere Log-Rekorder", data.getString("LogRecorderManager.init"));
80+
assertEquals("Initialisiere Log-Rekorder", data.getString("LogRecorderManager.init"));
7681
}
7782

83+
// variant testing
7884
@Issue("JENKINS-39034")
79-
@Test // variant testing
80-
public void test_valid_region_variant() throws IOException, SAXException {
85+
@Test
86+
void test_valid_region_variant() throws IOException {
8187
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=en_AU_variant").getJSONObject();
82-
Assert.assertEquals("ok", response.getString("status"));
88+
assertEquals("ok", response.getString("status"));
8389
JSONObject data = response.getJSONObject("data");
84-
Assert.assertEquals("value_au_variant", data.getString("Key"));
90+
assertEquals("value_au_variant", data.getString("Key"));
8591
}
8692

93+
//country testing with delimiter '-' instead of '_'
8794
@Issue("JENKINS-39034")
88-
@Test //country testing with delimiter '-' instead of '_'
89-
public void test_valid_region() throws IOException, SAXException {
95+
@Test
96+
void test_valid_region() throws IOException {
9097
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=en-AU").getJSONObject();
91-
Assert.assertEquals("ok", response.getString("status"));
98+
assertEquals("ok", response.getString("status"));
9299
JSONObject data = response.getJSONObject("data");
93-
Assert.assertEquals("value_au", data.getString("Key"));
100+
assertEquals("value_au", data.getString("Key"));
94101
}
95102

103+
//fallthrough to default language if variant does not exit
96104
@Issue("JENKINS-39034")
97-
@Test //fallthrough to default language if variant does not exit
98-
public void test_valid_fallback() throws IOException, SAXException {
105+
@Test
106+
void test_valid_fallback() throws IOException {
99107
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=en_NZ_variant").getJSONObject();
100-
Assert.assertEquals("ok", response.getString("status"));
108+
assertEquals("ok", response.getString("status"));
101109
JSONObject data = response.getJSONObject("data");
102-
Assert.assertEquals("value", data.getString("Key"));
110+
assertEquals("value", data.getString("Key"));
103111
}
104112

105-
@Test // testing with unknown language falls through to default language
106-
public void test_unsupported_language() throws IOException, SAXException {
113+
// testing with unknown language falls through to default language
114+
@Test
115+
void test_unsupported_language() throws IOException {
107116
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=xyz").getJSONObject();
108-
Assert.assertEquals("ok", response.getString("status"));
117+
assertEquals("ok", response.getString("status"));
109118
JSONObject data = response.getJSONObject("data");
110-
Assert.assertEquals("value", data.getString("Key"));
119+
assertEquals("value", data.getString("Key"));
111120
}
112121

113122
}

test/src/test/java/jenkins/bugs/BridgeMethodsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import java.util.List;
1515
import java.util.concurrent.Future;
1616
import java.util.stream.Collectors;
17-
import org.junit.Test;
17+
import org.junit.jupiter.api.Test;
1818
import org.jvnet.hudson.test.Issue;
1919

20-
public class BridgeMethodsTest {
20+
class BridgeMethodsTest {
2121

2222
@Test
2323
@Issue("JENKINS-65605")
24-
public void checkBridgeMethod() {
24+
void checkBridgeMethod() {
2525
/*
2626
* we should have 2 methods getFuture() in hudson.model.Queue$WaitingItem but with different return types :
2727
* hudson.model.Queue$WaitingItem.getFuture()Ljava/util/concurrent/Future

test/src/test/java/jenkins/bugs/Jenkins14749Test.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,65 @@
11
package jenkins.bugs;
22

33
import hudson.model.FreeStyleProject;
4+
import java.util.ArrayList;
5+
import java.util.List;
46
import org.htmlunit.cssparser.parser.CSSErrorHandler;
57
import org.htmlunit.cssparser.parser.CSSException;
68
import org.htmlunit.cssparser.parser.CSSParseException;
7-
import org.junit.Rule;
8-
import org.junit.Test;
9-
import org.junit.rules.ErrorCollector;
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
1012
import org.jvnet.hudson.test.JenkinsRule;
13+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
14+
import org.opentest4j.MultipleFailuresError;
1115

12-
public class Jenkins14749Test {
16+
@WithJenkins
17+
class Jenkins14749Test {
1318

14-
@Rule
15-
public JenkinsRule j = new JenkinsRule();
19+
private JenkinsRule j;
1620

17-
@Rule
18-
public ErrorCollector errors = new ErrorCollector();
21+
private final List<Throwable> errors = new ArrayList<>();
22+
23+
@BeforeEach
24+
void setUp(JenkinsRule rule) {
25+
j = rule;
26+
}
27+
28+
@AfterEach
29+
void tearDown() {
30+
if (!errors.isEmpty()) {
31+
throw new MultipleFailuresError(null, errors);
32+
}
33+
}
1934

2035
@Test
21-
public void dashboard() throws Exception {
36+
void dashboard() throws Exception {
2237
JenkinsRule.WebClient webClient = createErrorReportingWebClient();
2338
webClient.goTo("");
2439
}
2540

2641
@Test
27-
public void project() throws Exception {
42+
void project() throws Exception {
2843
FreeStyleProject p = j.createFreeStyleProject();
2944
JenkinsRule.WebClient webClient = createErrorReportingWebClient();
3045
webClient.getPage(p);
3146
}
3247

3348
@Test
34-
public void configureProject() throws Exception {
49+
void configureProject() throws Exception {
3550
FreeStyleProject p = j.createFreeStyleProject();
3651
JenkinsRule.WebClient webClient = createErrorReportingWebClient();
3752
webClient.getPage(p, "configure");
3853
}
3954

4055
@Test
41-
public void manage() throws Exception {
56+
void manage() throws Exception {
4257
JenkinsRule.WebClient webClient = createErrorReportingWebClient();
4358
webClient.goTo("manage");
4459
}
4560

4661
@Test
47-
public void system() throws Exception {
62+
void system() throws Exception {
4863
JenkinsRule.WebClient webClient = createErrorReportingWebClient();
4964
webClient.goTo("manage/configure");
5065
}
@@ -54,17 +69,17 @@ private JenkinsRule.WebClient createErrorReportingWebClient() {
5469
webClient.setCssErrorHandler(new CSSErrorHandler() {
5570
@Override
5671
public void warning(final CSSParseException exception) throws CSSException {
57-
errors.addError(exception);
72+
errors.add(exception);
5873
}
5974

6075
@Override
6176
public void error(final CSSParseException exception) throws CSSException {
62-
errors.addError(exception);
77+
errors.add(exception);
6378
}
6479

6580
@Override
6681
public void fatalError(final CSSParseException exception) throws CSSException {
67-
errors.addError(exception);
82+
errors.add(exception);
6883
}
6984
});
7085
return webClient;

0 commit comments

Comments
 (0)