Skip to content

Commit 7472190

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 5054f32 commit 7472190

File tree

69 files changed

+1613
-1518
lines changed

Some content is hidden

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

69 files changed

+1613
-1518
lines changed

pom.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<spotbugs.effort>Max</spotbugs.effort>
5252
<spotbugs.threshold>Low</spotbugs.threshold>
5353
<spotless.check.skip>false</spotless.check.skip>
54+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
5455
</properties>
5556

5657
<dependencyManagement>
@@ -261,13 +262,7 @@
261262
</dependency>
262263
<dependency>
263264
<groupId>org.mockito</groupId>
264-
<artifactId>mockito-core</artifactId>
265-
<scope>test</scope>
266-
</dependency>
267-
<dependency>
268-
<groupId>pl.pragmatists</groupId>
269-
<artifactId>JUnitParams</artifactId>
270-
<version>1.1.1</version>
265+
<artifactId>mockito-junit-jupiter</artifactId>
271266
<scope>test</scope>
272267
</dependency>
273268
</dependencies>

src/test/java/org/csanchez/jenkins/plugins/kubernetes/AbstractGoldenFileTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
package org.csanchez.jenkins.plugins.kubernetes;
22

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

55
import edu.umd.cs.findbugs.annotations.NonNull;
66
import io.fabric8.kubernetes.api.model.Pod;
77
import io.fabric8.kubernetes.client.utils.Serialization;
8-
import java.io.IOException;
98
import java.nio.charset.StandardCharsets;
109
import org.apache.commons.io.IOUtils;
1110
import org.csanchez.jenkins.plugins.kubernetes.pod.decorator.PodDecorator;
12-
import org.junit.Before;
11+
import org.junit.jupiter.api.BeforeEach;
1312

1413
abstract class AbstractGoldenFileTest {
1514

1615
protected KubernetesCloud cloud;
1716
protected PodDecorator decorator;
1817

19-
@Before
20-
public void setUpCloud() {
18+
@BeforeEach
19+
void beforeEach() {
2120
decorator = newDecorator();
2221
cloud = new KubernetesCloud("test");
2322
}
2423

2524
protected abstract PodDecorator newDecorator();
2625

27-
protected void test(String name) throws IOException {
26+
protected void test(String name) throws Exception {
2827
var beforeYAML = loadFileAsStream(name + "-before.yaml");
2928
var before = Serialization.unmarshal(beforeYAML, Pod.class);
30-
assertEquals(name + "-before.yaml is not normalized", Serialization.asYaml(before), beforeYAML);
29+
assertEquals(Serialization.asYaml(before), beforeYAML, name + "-before.yaml is not normalized");
3130
var afterYAML = loadFileAsStream(name + "-after.yaml");
3231
var after = decorator.decorate(cloud, before);
33-
assertEquals(name + "-after.yaml processed", Serialization.asYaml(after), afterYAML);
32+
assertEquals(Serialization.asYaml(after), afterYAML, name + "-after.yaml processed");
3433
}
3534

3635
@NonNull
37-
private String loadFileAsStream(String name) throws IOException {
36+
private String loadFileAsStream(String name) throws Exception {
3837
var is = getClass().getResourceAsStream(getClass().getSimpleName() + "/" + name);
3938
if (is == null) {
4039
throw new IllegalStateException("Test file \"src/test/resources/"

src/test/java/org/csanchez/jenkins/plugins/kubernetes/ConfigMapVolumeTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@
1515

1616
package org.csanchez.jenkins.plugins.kubernetes;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
1920

2021
import org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume;
21-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2223

23-
public class ConfigMapVolumeTest {
24+
class ConfigMapVolumeTest {
2425

2526
@Test
26-
public void testNullSubPathValue() {
27+
void testNullSubPathValue() {
2728
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
2829
assertNull(configMapVolume.getSubPath());
2930
}
3031

3132
@Test
32-
public void testValidSubPathValue() {
33+
void testValidSubPathValue() {
3334
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
3435
configMapVolume.setSubPath("miSubpath");
35-
assertEquals(configMapVolume.getSubPath(), "miSubpath");
36+
assertEquals("miSubpath", configMapVolume.getSubPath());
3637
}
3738
}

src/test/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplateTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515

1616
package org.csanchez.jenkins.plugins.kubernetes;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
1920

2021
import hudson.util.FormValidation;
2122
import java.util.Collections;
22-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2324

24-
public class ContainerTemplateTest {
25+
class ContainerTemplateTest {
2526

2627
@Test
27-
public void testCopyConstructorCreatesEqualInstance() {
28+
void testCopyConstructorCreatesEqualInstance() {
2829
ContainerTemplate originalTemplate = new ContainerTemplate("myname", "myimage");
2930
originalTemplate.setPrivileged(true);
3031
originalTemplate.setAlwaysPullImage(true);
@@ -43,31 +44,31 @@ public void testCopyConstructorCreatesEqualInstance() {
4344

4445
ContainerTemplate clonedTemplate = new ContainerTemplate(originalTemplate);
4546

46-
assertEquals("Cloned ContainerTemplate is not equal to the original one!", originalTemplate, clonedTemplate);
47+
assertEquals(originalTemplate, clonedTemplate, "Cloned ContainerTemplate is not equal to the original one!");
4748
assertEquals(
48-
"String representation (toString()) of the cloned and original ContainerTemplate is not equal!",
4949
originalTemplate.toString(),
50-
clonedTemplate.toString());
50+
clonedTemplate.toString(),
51+
"String representation (toString()) of the cloned and original ContainerTemplate is not equal!");
5152
}
5253

5354
@SuppressWarnings("ResultOfObjectAllocationIgnored")
5455
@Test
55-
public void badImage() throws Exception {
56+
void badImage() {
5657
new ContainerTemplate("n", "something");
5758
assertEquals(FormValidation.Kind.OK, new ContainerTemplate.DescriptorImpl().doCheckImage("something").kind);
5859
for (String empty : new String[] {null, ""}) {
59-
assertThrows("rejected " + empty, IllegalArgumentException.class, () -> new ContainerTemplate("n", empty));
60+
assertThrows(IllegalArgumentException.class, () -> new ContainerTemplate("n", empty), "rejected " + empty);
6061
assertEquals(
61-
"tolerating " + empty + " during form validation",
6262
FormValidation.Kind.OK,
63-
new ContainerTemplate.DescriptorImpl().doCheckImage(empty).kind);
63+
new ContainerTemplate.DescriptorImpl().doCheckImage(empty).kind,
64+
"tolerating " + empty + " during form validation");
6465
}
6566
for (String bad : new String[] {" ", " something"}) {
66-
assertThrows("rejected " + bad, IllegalArgumentException.class, () -> new ContainerTemplate("n", bad));
67+
assertThrows(IllegalArgumentException.class, () -> new ContainerTemplate("n", bad), "rejected " + bad);
6768
assertEquals(
68-
"rejected " + bad,
6969
FormValidation.Kind.ERROR,
70-
new ContainerTemplate.DescriptorImpl().doCheckImage(bad).kind);
70+
new ContainerTemplate.DescriptorImpl().doCheckImage(bad).kind,
71+
"rejected " + bad);
7172
}
7273
}
7374
}

src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubectlBuildWrapperTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.deletePods;
44
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.getLabels;
5-
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
66

77
import com.cloudbees.plugins.credentials.CredentialsScope;
88
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
99
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
1010
import hudson.model.Result;
1111
import org.csanchez.jenkins.plugins.kubernetes.pipeline.AbstractKubernetesPipelineTest;
12-
import org.junit.Before;
13-
import org.junit.Test;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
1414

15-
public class KubectlBuildWrapperTest extends AbstractKubernetesPipelineTest {
16-
@Before
17-
public void setUp() throws Exception {
15+
class KubectlBuildWrapperTest extends AbstractKubernetesPipelineTest {
16+
17+
@BeforeEach
18+
void beforeEach() throws Exception {
1819
deletePods(cloud.connect(), getLabels(cloud, this, name), false);
1920
assertNotNull(createJobThenScheduleRun());
2021
UsernamePasswordCredentialsImpl creds = new UsernamePasswordCredentialsImpl(
@@ -23,13 +24,13 @@ public void setUp() throws Exception {
2324
}
2425

2526
@Test
26-
public void kubectlBuildWrapper_missingCredentials() throws Exception {
27+
void kubectlBuildWrapper_missingCredentials() throws Exception {
2728
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
2829
r.assertLogContains("No credentials found for id \"abcd\"", b);
2930
}
3031

3132
@Test
32-
public void kubectlBuildWrapper_invalidCredentials() throws Exception {
33+
void kubectlBuildWrapper_invalidCredentials() throws Exception {
3334
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
3435
r.assertLogContains("Unable to connect to the server", b);
3536
}

src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProviderTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323
*/
2424
package org.csanchez.jenkins.plugins.kubernetes;
2525

26-
import static org.junit.Assert.assertEquals;
27-
2826
import java.util.function.Consumer;
2927
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Always;
30-
import org.junit.Assert;
31-
import org.junit.Test;
28+
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.Test;
3230

33-
public class KubernetesClientProviderTest {
31+
class KubernetesClientProviderTest {
3432

3533
@Test
36-
public void testGetValidity() {
34+
void testGetValidity() {
3735
KubernetesCloud cloud = new KubernetesCloud("foo");
3836
// changes to these properties should trigger different validity value
3937
checkValidityChanges(
@@ -60,15 +58,16 @@ public void testGetValidity() {
6058
c -> c.setDirectConnection(true));
6159

6260
// verify stability
63-
assertEquals(KubernetesClientProvider.getValidity(cloud), KubernetesClientProvider.getValidity(cloud));
61+
Assertions.assertEquals(
62+
KubernetesClientProvider.getValidity(cloud), KubernetesClientProvider.getValidity(cloud));
6463
}
6564

6665
private void checkValidityChanges(KubernetesCloud cloud, Consumer<KubernetesCloud>... mutations) {
67-
checkValidity(cloud, Assert::assertNotEquals, mutations);
66+
checkValidity(cloud, Assertions::assertNotEquals, mutations);
6867
}
6968

7069
private void checkValidityDoesNotChange(KubernetesCloud cloud, Consumer<KubernetesCloud>... mutations) {
71-
checkValidity(cloud, Assert::assertEquals, mutations);
70+
checkValidity(cloud, Assertions::assertEquals, mutations);
7271
}
7372

7473
private void checkValidity(
@@ -78,12 +77,12 @@ private void checkValidity(
7877
for (Consumer<KubernetesCloud> mut : mutations) {
7978
mut.accept(cloud);
8079
int after = KubernetesClientProvider.getValidity(cloud);
81-
validityAssertion.doAssert("change #" + count++ + " of " + mutations.length, v, after);
80+
validityAssertion.doAssert(v, after, "change #" + count++ + " of " + mutations.length);
8281
v = after;
8382
}
8483
}
8584

8685
interface ValidityAssertion {
87-
void doAssert(String message, int before, int after);
86+
void doAssert(int before, int after, String message);
8887
}
8988
}

src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudFIPSTest.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,80 @@
44
import static org.hamcrest.Matchers.containsString;
55
import static org.hamcrest.Matchers.notNullValue;
66
import static org.hamcrest.Matchers.nullValue;
7-
import static org.junit.Assert.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
88

99
import hudson.ExtensionList;
1010
import io.jenkins.cli.shaded.org.apache.commons.io.FileUtils;
11-
import java.io.IOException;
1211
import java.nio.charset.Charset;
1312
import java.nio.file.Paths;
1413
import jenkins.security.FIPS140;
15-
import org.junit.ClassRule;
16-
import org.junit.Rule;
17-
import org.junit.Test;
18-
import org.jvnet.hudson.test.FlagRule;
14+
import org.junit.jupiter.api.AfterAll;
15+
import org.junit.jupiter.api.BeforeAll;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
1918
import org.jvnet.hudson.test.Issue;
2019
import org.jvnet.hudson.test.JenkinsRule;
20+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2121
import org.jvnet.hudson.test.recipes.LocalData;
2222

23-
public class KubernetesCloudFIPSTest {
23+
@WithJenkins
24+
class KubernetesCloudFIPSTest {
2425

25-
@ClassRule
26-
public static FlagRule<String> fipsFlag = FlagRule.systemProperty(FIPS140.class.getName() + ".COMPLIANCE", "true");
26+
private static String fipsFlag;
2727

28-
@Rule
29-
public JenkinsRule r = new JenkinsRule();
28+
private JenkinsRule r;
29+
30+
@BeforeAll
31+
static void beforeAll() {
32+
fipsFlag = System.setProperty(FIPS140.class.getName() + ".COMPLIANCE", "true");
33+
}
34+
35+
@BeforeEach
36+
void beforeEach(JenkinsRule rule) {
37+
r = rule;
38+
}
39+
40+
@AfterAll
41+
static void afterAll() {
42+
if (fipsFlag != null) {
43+
System.setProperty(FIPS140.class.getName() + ".COMPLIANCE", fipsFlag);
44+
} else {
45+
System.clearProperty(FIPS140.class.getName() + ".COMPLIANCE");
46+
}
47+
}
3048

3149
@Test
3250
@Issue("JENKINS-73460")
33-
public void onlyFipsCompliantValuesAreAcceptedTest() throws IOException {
51+
void onlyFipsCompliantValuesAreAcceptedTest() throws Exception {
3452
KubernetesCloud cloud = new KubernetesCloud("test-cloud");
3553
assertThrows(IllegalArgumentException.class, () -> cloud.setSkipTlsVerify(true));
3654
cloud.setSkipTlsVerify(false);
3755
assertThrows(IllegalArgumentException.class, () -> cloud.setServerUrl("http://example.org"));
3856
cloud.setServerUrl("https://example.org");
3957
assertThrows(
40-
"Invalid certificates throw exception",
4158
IllegalArgumentException.class,
42-
() -> cloud.setServerCertificate(getCert("not-a-cert")));
59+
() -> cloud.setServerCertificate(getCert("not-a-cert")),
60+
"Invalid certificates throw exception");
4361
Throwable exception = assertThrows(
44-
"Invalid length", IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("rsa1024")));
62+
IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("rsa1024")), "Invalid length");
4563
assertThat(exception.getLocalizedMessage(), containsString("2048"));
4664
cloud.setServerCertificate(getCert("rsa2048"));
4765
exception = assertThrows(
48-
"invalid length", IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("dsa1024")));
66+
IllegalArgumentException.class, () -> cloud.setServerCertificate(getCert("dsa1024")), "invalid length");
4967
assertThat(exception.getLocalizedMessage(), containsString("2048"));
5068
cloud.setServerCertificate(getCert("dsa2048"));
5169
exception = assertThrows(
52-
"Invalid field size",
5370
IllegalArgumentException.class,
54-
() -> cloud.setServerCertificate(getCert("ecdsa192")));
71+
() -> cloud.setServerCertificate(getCert("ecdsa192")),
72+
"Invalid field size");
5573
assertThat(exception.getLocalizedMessage(), containsString("224"));
5674
cloud.setServerCertificate(getCert("ecdsa224"));
5775
}
5876

5977
@Test
6078
@Issue("JENKINS-73460")
6179
@LocalData
62-
public void nonCompliantCloudsAreCleanedTest() {
80+
void nonCompliantCloudsAreCleanedTest() {
6381
assertThat("compliant-cloud is loaded", r.jenkins.getCloud("compliant-cloud"), notNullValue());
6482
assertThat(
6583
"no certificate is a valid cloud",
@@ -72,7 +90,7 @@ public void nonCompliantCloudsAreCleanedTest() {
7290

7391
@Test
7492
@Issue("JENKINS-73460")
75-
public void formValidationTest() throws IOException {
93+
void formValidationTest() throws Exception {
7694
ExtensionList<KubernetesCloud.DescriptorImpl> descriptors =
7795
ExtensionList.lookup(KubernetesCloud.DescriptorImpl.class);
7896
KubernetesCloud.DescriptorImpl descriptor = descriptors.stream()
@@ -109,7 +127,7 @@ public void formValidationTest() throws IOException {
109127
notNullValue());
110128
}
111129

112-
private String getCert(String alg) throws IOException {
130+
private String getCert(String alg) throws Exception {
113131
return FileUtils.readFileToString(
114132
Paths.get("src/test/resources/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudFIPSTest/certs")
115133
.resolve(alg)

0 commit comments

Comments
 (0)