Skip to content

Commit 115ae63

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 58f1239 commit 115ae63

File tree

4 files changed

+40
-43
lines changed

4 files changed

+40
-43
lines changed

Diff for: pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<licenses>
2121
<license>
2222
<name>MIT License</name>
23-
<url>http://opensource.org/licenses/MIT</url>
23+
<url>https://opensource.org/licenses/MIT</url>
2424
</license>
2525
</licenses>
2626

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package hudson.plugins.locale;
22

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

56
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
67
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
8+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
79
import jenkins.model.Jenkins;
8-
import org.junit.Rule;
9-
import org.junit.Test;
10+
import org.junit.jupiter.api.Test;
1011

11-
public class ConfigurationAsCodeTest {
12-
13-
@Rule
14-
public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
12+
@WithJenkinsConfiguredWithCode
13+
class ConfigurationAsCodeTest {
1514

1615
@Test
1716
@ConfiguredWithCode("configuration-as-code.yml")
18-
public void should_support_configuration_as_code() throws Exception {
17+
void should_support_configuration_as_code(JenkinsConfiguredWithCodeRule r) {
1918
PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0);
2019
assertEquals("fr", plugin.getSystemLocale());
21-
assertEquals(true, plugin.isIgnoreAcceptLanguage());
20+
assertTrue(plugin.isIgnoreAcceptLanguage());
2221
}
2322
}

Diff for: src/test/java/hudson/plugins/locale/MigrationTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
package hudson.plugins.locale;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
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;
56

67
import jenkins.model.Jenkins;
7-
import org.junit.Rule;
8-
import org.junit.Test;
8+
import org.junit.jupiter.api.Test;
99
import org.jvnet.hudson.test.JenkinsRule;
10+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1011
import org.jvnet.hudson.test.recipes.LocalData;
1112

1213
/**
1314
* Tests data loading from Locale Plugin 1.3.
1415
* @author Oleg Nenashev
1516
*/
16-
public class MigrationTest {
17-
18-
@Rule
19-
public JenkinsRule j = new JenkinsRule();
17+
@WithJenkins
18+
class MigrationTest {
2019

2120
@LocalData
2221
@Test
23-
public void dataMigration_13() {
22+
void dataMigration_13(JenkinsRule j) {
2423
PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0);
2524
assertEquals("en-US", plugin.getSystemLocale());
26-
assertEquals(true, plugin.isIgnoreAcceptLanguage());
25+
assertTrue(plugin.isIgnoreAcceptLanguage());
2726
}
2827

2928
@LocalData
3029
@Test
31-
public void dataMigration_UnsetLocale() {
30+
void dataMigration_UnsetLocale(JenkinsRule j) {
3231
PluginImpl plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0);
3332

3433
// Assuming the default behavior if systemLocale is unset
+21-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package hudson.plugins.locale;
22

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

67
import hudson.util.ListBoxModel;
78
import java.util.Arrays;
89
import java.util.HashSet;
910
import java.util.Locale;
1011
import java.util.Set;
1112
import jenkins.model.Jenkins;
12-
import org.junit.Before;
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;
16+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1617

17-
public class PluginImplTest {
18-
19-
@Rule
20-
public JenkinsRule j = new JenkinsRule();
18+
@WithJenkins
19+
class PluginImplTest {
2120

2221
private PluginImpl plugin;
2322

24-
@Before
25-
public void setUp() {
23+
@BeforeEach
24+
void setUp(JenkinsRule j) {
2625
plugin = Jenkins.get().getExtensionList(PluginImpl.class).get(0);
2726
}
2827

@@ -33,26 +32,26 @@ public void setUp() {
3332
"zh_TW"));
3433

3534
@Test
36-
public void testDoFillSystemLocaleItems() {
35+
void testDoFillSystemLocaleItems() {
3736
// Invoke the method
3837
ListBoxModel model = plugin.doFillSystemLocaleItems();
3938

4039
// Expected size of the ListBoxModel
4140
int expectedSize = ALLOWED_LOCALES.size() + 1; // +1 for the "Use Default Locale" option
4241

4342
// Verify the returned ListBoxModel size
44-
assertEquals("The returned ListBoxModel size is not as expected", expectedSize, model.size());
43+
assertEquals(expectedSize, model.size(), "The returned ListBoxModel size is not as expected");
4544

4645
// Verify that the first option is "Use Default Locale"
4746
String expectedFirstOption = String.format(
4847
"Use Default Locale - %s (%s)",
4948
Locale.getDefault().getDisplayName(), Locale.getDefault().toString());
50-
assertEquals("The first option should be 'Use Default Locale'", expectedFirstOption, model.get(0).name);
49+
assertEquals(expectedFirstOption, model.get(0).name, "The first option should be 'Use Default Locale'");
5150

5251
// Verify that the allowed locales are correctly added to the ListBoxModel, excluding the first option
5352
for (String localeStr : ALLOWED_LOCALES) {
5453
Locale locale = Locale.forLanguageTag(localeStr.replace('_', '-'));
55-
String expectedOption = String.format("%s - %s", locale.getDisplayName(), locale.toString());
54+
String expectedOption = String.format("%s - %s", locale.getDisplayName(), locale);
5655

5756
boolean found = false;
5857
for (int i = 1; i < model.size(); i++) { // Start from 1 to skip the "Use Default Locale" option
@@ -61,37 +60,37 @@ public void testDoFillSystemLocaleItems() {
6160
break;
6261
}
6362
}
64-
assertEquals("The ListBoxModel does not contain the expected locale: " + locale, true, found);
63+
assertTrue(found, "The ListBoxModel does not contain the expected locale: " + locale);
6564
}
6665
}
6766

6867
@Test
69-
public void testSetSystemLocale() {
68+
void testSetSystemLocale() {
7069
// Test setting systemLocale
7170
String systemLocale = "en_US";
7271
plugin.setSystemLocale(systemLocale);
7372
assertEquals(systemLocale, plugin.getSystemLocale());
7473
}
7574

7675
@Test
77-
public void testSetIgnoreAcceptLanguage() {
76+
void testSetIgnoreAcceptLanguage() {
7877
// Test setting ignoreAcceptLanguage
7978
boolean ignoreAcceptLanguage = true;
8079
plugin.setIgnoreAcceptLanguage(ignoreAcceptLanguage);
8180
assertEquals(ignoreAcceptLanguage, plugin.isIgnoreAcceptLanguage());
8281
}
8382

8483
@Test
85-
public void testNullSystemLocale() {
84+
void testNullSystemLocale() {
8685
// Test setting systemLocale to null
8786
plugin.setSystemLocale(null);
88-
assertNull("System locale should be null", plugin.getSystemLocale());
87+
assertNull(plugin.getSystemLocale(), "System locale should be null");
8988
}
9089

9190
@Test
92-
public void testEmptySystemLocale() {
91+
void testEmptySystemLocale() {
9392
// Test setting systemLocale to empty string
9493
plugin.setSystemLocale("");
95-
assertNull("System locale should be empty", plugin.getSystemLocale());
94+
assertNull(plugin.getSystemLocale(), "System locale should be empty");
9695
}
9796
}

0 commit comments

Comments
 (0)