Skip to content

Commit d65aa17

Browse files
Migrate tests to JUnit5 (test / misc / 4)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 22ae41d commit d65aa17

35 files changed

+604
-371
lines changed

src/checkstyle/checkstyle-configuration.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<property name="max" value="240"/>
1111
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
1212
</module>
13+
<module name="SuppressWarningsFilter" />
1314
<module name="TreeWalker">
1415
<!--
1516
Annotations: https://checkstyle.sourceforge.io/config_annotation.html
@@ -38,6 +39,7 @@
3839
<module name="StringLiteralEquality"/>
3940
<module name="SuperClone"/>
4041
<module name="SuperFinalize"/>
42+
<module name="SuppressWarningsHolder" />
4143
<module name="UnnecessarySemicolonAfterOuterTypeDeclaration"/>
4244
<module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
4345
<module name="UnnecessarySemicolonInEnumeration"/>

test/src/test/java/benchmarks/BenchmarkTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package benchmarks;
22

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

55
import java.nio.file.Files;
66
import java.nio.file.Paths;
77
import java.util.concurrent.TimeUnit;
8-
import org.junit.Test;
8+
import org.junit.jupiter.api.Test;
99
import org.openjdk.jmh.annotations.Mode;
1010
import org.openjdk.jmh.results.format.ResultFormatType;
1111
import org.openjdk.jmh.runner.Runner;
1212
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
1313
import org.openjdk.jmh.runner.options.OptionsBuilder;
1414

15-
public class BenchmarkTest {
15+
class BenchmarkTest {
1616
/**
1717
* Runs a sample benchmark to make sure that benchmarks execute successfully and generate a report.
1818
* <p>
@@ -23,7 +23,7 @@ public class BenchmarkTest {
2323
* @see <a href="https://www.jenkins.io/blog/2019/06/21/performance-testing-jenkins/">Blog post on writing benchmarks</a>
2424
*/
2525
@Test
26-
public void runSampleBenchmark() throws Exception {
26+
void runSampleBenchmark() throws Exception {
2727
// run the minimum possible number of iterations
2828
ChainedOptionsBuilder options = new OptionsBuilder()
2929
.mode(Mode.AverageTime)

test/src/test/java/lib/form/AdvancedButtonTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package lib.form;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
5-
import static org.junit.Assert.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import hudson.model.InvisibleAction;
88
import hudson.model.RootAction;
@@ -11,24 +11,31 @@
1111
import org.htmlunit.html.HtmlForm;
1212
import org.htmlunit.html.HtmlFormUtil;
1313
import org.htmlunit.html.HtmlPage;
14-
import org.junit.Rule;
15-
import org.junit.Test;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
1616
import org.jvnet.hudson.test.Issue;
1717
import org.jvnet.hudson.test.JenkinsRule;
1818
import org.jvnet.hudson.test.TestExtension;
19+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1920
import org.kohsuke.stapler.StaplerRequest2;
2021

2122
/**
2223
*
2324
*
2425
* @author Kohsuke Kawaguchi
2526
*/
26-
public class AdvancedButtonTest {
27+
@WithJenkins
28+
class AdvancedButtonTest {
2729

28-
@Rule public JenkinsRule j = new JenkinsRule();
30+
private JenkinsRule j;
31+
32+
@BeforeEach
33+
void setUp(JenkinsRule rule) {
34+
j = rule;
35+
}
2936

3037
@Test
31-
public void testNestedOptionalBlock() throws Exception {
38+
void testNestedOptionalBlock() throws Exception {
3239
HtmlPage page = j.createWebClient().goTo("self/testNestedOptionalBlock");
3340
HtmlForm form = page.getFormByName("config");
3441
HtmlFormUtil.getButtonByCaption(form, "Advanced").click();
@@ -39,7 +46,7 @@ public void testNestedOptionalBlock() throws Exception {
3946

4047
@Issue("JENKINS-14632")
4148
@Test
42-
public void testSectionInsideOfAdvanced() throws Exception {
49+
void testSectionInsideOfAdvanced() throws Exception {
4350
HtmlPage page = j.createWebClient().goTo("self/testSectionInsideOfAdvanced");
4451
HtmlForm form = page.getFormByName("config");
4552
assertFalse(form.getInputByName("b").isDisplayed());

test/src/test/java/lib/form/ApplyButtonTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package lib.form;
22

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

55
import hudson.markup.RawHtmlMarkupFormatter;
66
import hudson.model.FreeStyleBuild;
@@ -9,22 +9,28 @@
99
import org.apache.commons.io.IOUtils;
1010
import org.htmlunit.html.HtmlForm;
1111
import org.htmlunit.html.HtmlPage;
12-
import org.junit.Rule;
13-
import org.junit.Test;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
1414
import org.jvnet.hudson.test.Issue;
1515
import org.jvnet.hudson.test.JenkinsRule;
16+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1617

17-
public class ApplyButtonTest {
18+
@WithJenkins
19+
class ApplyButtonTest {
1820

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

2228
/**
2329
* Editing code mirror should still gets reflected when you click apply.
2430
*/
2531
@Test
2632
@Issue("JENKINS-18436")
27-
public void editDescription() throws Exception {
33+
void editDescription() throws Exception {
2834
j.jenkins.setMarkupFormatter(RawHtmlMarkupFormatter.INSTANCE); // need something using CodeMirror
2935
FreeStyleProject p = j.createFreeStyleProject();
3036
FreeStyleBuild b = j.buildAndAssertSuccess(p);

test/src/test/java/lib/form/BooleanRadioTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@
1111
import net.sf.json.JSONObject;
1212
import org.htmlunit.html.HtmlForm;
1313
import org.htmlunit.html.HtmlPage;
14-
import org.junit.Rule;
15-
import org.junit.Test;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
1616
import org.jvnet.hudson.test.JenkinsRule;
1717
import org.jvnet.hudson.test.TestExtension;
18+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1819
import org.kohsuke.stapler.DataBoundConstructor;
1920
import org.kohsuke.stapler.StaplerRequest2;
2021

21-
public class BooleanRadioTest {
22+
@WithJenkins
23+
class BooleanRadioTest {
2224

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

2632
@Test
27-
public void test() throws Exception {
33+
void test() throws Exception {
2834
HtmlPage p = j.createWebClient().goTo("self/test1");
2935
HtmlForm f = p.getFormByName("config");
3036
f.getInputByName("_.f").setChecked(true);
@@ -35,6 +41,7 @@ public static final class BooleanRadioTestDescribable implements Describable<Boo
3541

3642
boolean f;
3743

44+
@SuppressWarnings("checkstyle:redundantmodifier")
3845
@DataBoundConstructor
3946
public BooleanRadioTestDescribable(boolean f) {
4047
this.f = f;

test/src/test/java/lib/form/ComboBoxTest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
package lib.form;
2626

27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.fail;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.fail;
2929

3030
import edu.umd.cs.findbugs.annotations.NonNull;
3131
import hudson.Extension;
@@ -42,20 +42,27 @@
4242
import org.htmlunit.html.HtmlElement;
4343
import org.htmlunit.html.HtmlElementUtil;
4444
import org.htmlunit.html.HtmlPage;
45-
import org.junit.Rule;
46-
import org.junit.Test;
45+
import org.junit.jupiter.api.BeforeEach;
46+
import org.junit.jupiter.api.Test;
4747
import org.jvnet.hudson.test.Issue;
4848
import org.jvnet.hudson.test.JenkinsRule;
4949
import org.jvnet.hudson.test.TestExtension;
50+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
5051
import org.kohsuke.stapler.DataBoundConstructor;
5152
import org.kohsuke.stapler.QueryParameter;
5253

5354
/**
5455
* @author John McNally
5556
*/
56-
public class ComboBoxTest {
57+
@WithJenkins
58+
class ComboBoxTest {
5759

58-
@Rule public JenkinsRule j = new JenkinsRule();
60+
private JenkinsRule j;
61+
62+
@BeforeEach
63+
void setUp(JenkinsRule rule) {
64+
j = rule;
65+
}
5966

6067
/**
6168
* Used in testCompoundFieldDependentCombobox for Issue("JENKINS-16719")
@@ -64,6 +71,7 @@ public static class CompoundFieldComboBoxBuilder extends Publisher {
6471
private CompoundField compoundField;
6572
private String foo;
6673

74+
@SuppressWarnings("checkstyle:redundantmodifier")
6775
@DataBoundConstructor
6876
public CompoundFieldComboBoxBuilder(CompoundField compoundField, String foo) {
6977
this.compoundField = compoundField;
@@ -105,6 +113,7 @@ public static class CompoundField implements Describable<CompoundField> {
105113
private final String abc;
106114
private final String xyz;
107115

116+
@SuppressWarnings("checkstyle:redundantmodifier")
108117
@DataBoundConstructor
109118
public CompoundField(String abc, String xyz) {
110119
this.abc = abc;
@@ -128,7 +137,7 @@ public static final class DescriptorImpl extends Descriptor<CompoundField> {}
128137
*/
129138
@Issue("JENKINS-16719")
130139
@Test
131-
public void testCompoundFieldDependentComboBox() throws Exception {
140+
void testCompoundFieldDependentComboBox() throws Exception {
132141
Descriptor d1 = new CompoundFieldComboBoxBuilder.DescriptorImpl();
133142
Publisher.all().add(d1);
134143
Descriptor d2 = new CompoundField.DescriptorImpl();
@@ -168,7 +177,7 @@ public ComboBoxModel doFillXssItems() {
168177

169178
@Issue("SECURITY-1525")
170179
@Test
171-
public void testEnsureXssNotPossible() throws Exception {
180+
void testEnsureXssNotPossible() throws Exception {
172181
XssProperty xssProperty = new XssProperty();
173182
FreeStyleProject p = j.createFreeStyleProject();
174183
p.addProperty(xssProperty);

test/src/test/java/lib/form/DropdownListTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,28 @@
66
import net.sf.json.JSONObject;
77
import org.htmlunit.html.HtmlForm;
88
import org.htmlunit.html.HtmlPage;
9-
import org.junit.Rule;
10-
import org.junit.Test;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
1111
import org.jvnet.hudson.test.JenkinsRule;
1212
import org.jvnet.hudson.test.TestExtension;
13+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1314
import org.kohsuke.stapler.StaplerRequest2;
1415

1516
/**
1617
* @author Kohsuke Kawaguchi
1718
*/
18-
public class DropdownListTest {
19+
@WithJenkins
20+
class DropdownListTest {
1921

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

2229
@Test
23-
public void test1() throws Exception {
30+
void test1() throws Exception {
2431
HtmlPage p = j.createWebClient().goTo("self/test1");
2532
HtmlForm f = p.getFormByName("config");
2633
j.submit(f);

test/src/test/java/lib/form/EnumSetTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@
1010
import net.sf.json.JSONObject;
1111
import org.htmlunit.html.HtmlForm;
1212
import org.htmlunit.html.HtmlPage;
13-
import org.junit.Rule;
14-
import org.junit.Test;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
1515
import org.jvnet.hudson.test.JenkinsRule;
1616
import org.jvnet.hudson.test.TestExtension;
17+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1718
import org.kohsuke.stapler.DataBoundConstructor;
1819
import org.kohsuke.stapler.StaplerRequest2;
1920

2021
/**
2122
* @author Kohsuke Kawaguchi
2223
*/
23-
public class EnumSetTest {
24+
@WithJenkins
25+
class EnumSetTest {
2426

25-
@Rule public JenkinsRule j = new JenkinsRule();
27+
private JenkinsRule j;
28+
29+
@BeforeEach
30+
void setUp(JenkinsRule rule) {
31+
j = rule;
32+
}
2633

2734
@Test
28-
public void test1() throws Exception {
35+
void test1() throws Exception {
2936
HtmlPage p = j.createWebClient().goTo("self/test1");
3037
HtmlForm f = p.getFormByName("config");
3138
j.submit(f);
@@ -35,6 +42,7 @@ public static final class EnumSetTestDescribable implements Describable<EnumSetT
3542

3643
EnumSet<BallColor> f;
3744

45+
@SuppressWarnings("checkstyle:redundantmodifier")
3846
@DataBoundConstructor
3947
public EnumSetTestDescribable(EnumSet<BallColor> colors) {
4048
f = colors;

test/src/test/java/lib/form/EnumTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package lib.form;
22

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

55
import hudson.Extension;
66
import hudson.model.BallColor;
@@ -13,18 +13,25 @@
1313
import org.htmlunit.html.HtmlForm;
1414
import org.htmlunit.html.HtmlPage;
1515
import org.htmlunit.html.HtmlSelect;
16-
import org.junit.Rule;
17-
import org.junit.Test;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
1818
import org.jvnet.hudson.test.JenkinsRule;
1919
import org.jvnet.hudson.test.TestExtension;
20+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2021
import org.kohsuke.stapler.StaplerRequest2;
2122

22-
public class EnumTest {
23-
@Rule
24-
public JenkinsRule rule = new JenkinsRule();
23+
@WithJenkins
24+
class EnumTest {
25+
26+
private JenkinsRule rule;
27+
28+
@BeforeEach
29+
void setUp(JenkinsRule j) {
30+
rule = j;
31+
}
2532

2633
@Test
27-
public void testSelectionNoDefault() throws Exception {
34+
void testSelectionNoDefault() throws Exception {
2835
HtmlForm form = getForm("noDefault");
2936
HtmlSelect select;
3037

@@ -38,7 +45,7 @@ public void testSelectionNoDefault() throws Exception {
3845
}
3946

4047
@Test
41-
public void testSelectionWithDefault() throws Exception {
48+
void testSelectionWithDefault() throws Exception {
4249
HtmlForm form = getForm("withDefault");
4350
HtmlSelect select;
4451

0 commit comments

Comments
 (0)