Skip to content

Commit 12c0ad4

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 0064b4a commit 12c0ad4

14 files changed

+348
-306
lines changed

pom.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<dependency>
8383
<groupId>io.jenkins.tools.bom</groupId>
8484
<artifactId>bom-${jenkins.baseline}.x</artifactId>
85-
<version>3944.v1a_e4f8b_452db_</version>
85+
<version>4136.vca_c3202a_7fd1</version>
8686
<type>pom</type>
8787
<scope>import</scope>
8888
</dependency>
@@ -251,11 +251,6 @@
251251
<groupId>org.jenkins-ci.plugins.workflow</groupId>
252252
<artifactId>workflow-step-api</artifactId>
253253
</dependency>
254-
<dependency>
255-
<groupId>junit</groupId>
256-
<artifactId>junit</artifactId>
257-
<scope>test</scope>
258-
</dependency>
259254
<!-- Required for InjectedTest. Git 4.4.5 for org.jvnet.hudson.test.PluginAutomaticTestBuilder$OtherTests.testPluginActive -->
260255
<dependency>
261256
<groupId>org.jenkins-ci.plugins</groupId>
@@ -269,7 +264,7 @@
269264
</dependency>
270265
<dependency>
271266
<groupId>org.mockito</groupId>
272-
<artifactId>mockito-core</artifactId>
267+
<artifactId>mockito-junit-jupiter</artifactId>
273268
<scope>test</scope>
274269
</dependency>
275270
</dependencies>

src/test/java/com/google/jenkins/plugins/k8sengine/ClusterUtilTest.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,61 @@
1616

1717
package com.google.jenkins.plugins.k8sengine;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

21-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2223

2324
/** Tests for verifying the behavior of {@link ClusterUtil methods} */
24-
public class ClusterUtilTest {
25-
@Test(expected = NullPointerException.class)
26-
public void testToNameAndLocationNullCluster() {
27-
ClusterUtil.toNameAndLocation(null);
25+
class ClusterUtilTest {
26+
27+
@Test
28+
void testToNameAndLocationNullCluster() {
29+
assertThrows(NullPointerException.class, () -> ClusterUtil.toNameAndLocation(null));
2830
}
2931

30-
@Test(expected = IllegalArgumentException.class)
31-
public void testToNameAndLocationNullName() {
32-
ClusterUtil.toNameAndLocation(null, "us-west1-a");
32+
@Test
33+
void testToNameAndLocationNullName() {
34+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.toNameAndLocation(null, "us-west1-a"));
3335
}
3436

35-
@Test(expected = IllegalArgumentException.class)
36-
public void testToNameAndLocationEmptyName() {
37-
ClusterUtil.toNameAndLocation("", "us-west1-a");
37+
@Test
38+
void testToNameAndLocationEmptyName() {
39+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.toNameAndLocation("", "us-west1-a"));
3840
}
3941

40-
@Test(expected = IllegalArgumentException.class)
41-
public void testToNameAndLocationNullLocation() {
42-
ClusterUtil.toNameAndLocation("test-cluster", null);
42+
@Test
43+
void testToNameAndLocationNullLocation() {
44+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.toNameAndLocation("test-cluster", null));
4345
}
4446

45-
@Test(expected = IllegalArgumentException.class)
46-
public void testToNameAndLocationEmptyLocation() {
47-
ClusterUtil.toNameAndLocation("test-cluster", "");
47+
@Test
48+
void testToNameAndLocationEmptyLocation() {
49+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.toNameAndLocation("test-cluster", ""));
4850
}
4951

5052
@Test
51-
public void testToNameAndLocationValidInputs() {
53+
void testToNameAndLocationValidInputs() {
5254
assertEquals("test-cluster (us-west1-a)", ClusterUtil.toNameAndLocation("test-cluster", "us-west1-a"));
5355
}
5456

55-
@Test(expected = IllegalArgumentException.class)
56-
public void testValuesFromNameAndLocationNullInput() {
57-
ClusterUtil.valuesFromNameAndLocation(null);
57+
@Test
58+
void testValuesFromNameAndLocationNullInput() {
59+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.valuesFromNameAndLocation(null));
5860
}
5961

60-
@Test(expected = IllegalArgumentException.class)
61-
public void testValuesFromNameAndLocationEmptyInput() {
62-
ClusterUtil.valuesFromNameAndLocation("");
62+
@Test
63+
void testValuesFromNameAndLocationEmptyInput() {
64+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.valuesFromNameAndLocation(""));
6365
}
6466

65-
@Test(expected = IllegalArgumentException.class)
66-
public void testValuesFromNameAndLocationMalformedInput() {
67-
ClusterUtil.valuesFromNameAndLocation("test us-west1-a");
67+
@Test
68+
void testValuesFromNameAndLocationMalformedInput() {
69+
assertThrows(IllegalArgumentException.class, () -> ClusterUtil.valuesFromNameAndLocation("test us-west1-a"));
6870
}
6971

7072
@Test
71-
public void testValuesFromNameAndLocationValidInput() {
73+
void testValuesFromNameAndLocationValidInput() {
7274
ClusterUtil.valuesFromNameAndLocation("test-cluster (us-west1-a)");
7375
}
7476
}
Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
package com.google.jenkins.plugins.k8sengine;
22

3-
import static org.junit.Assert.assertNotNull;
3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import com.cloudbees.plugins.credentials.CredentialsStore;
67
import com.cloudbees.plugins.credentials.SecretBytes;
78
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
89
import com.cloudbees.plugins.credentials.domains.Domain;
9-
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
1010
import com.google.api.client.auth.oauth2.Credential;
1111
import com.google.common.collect.ImmutableList;
1212
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
1313
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials;
1414
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig;
1515
import hudson.AbortException;
16-
import java.io.IOException;
1716
import java.nio.charset.StandardCharsets;
18-
import org.junit.ClassRule;
19-
import org.junit.Test;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
2019
import org.jvnet.hudson.test.JenkinsRule;
20+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
21+
22+
@WithJenkins
23+
class CredentialsUtilTest {
2124

22-
public class CredentialsUtilTest {
2325
private static final String TEST_CREDENTIALS_ID = "test-credentials-id";
2426
private static final String TEST_INVALID_CREDENTIALS_ID = "test-invalid-credentials-id";
25-
private static final String TEST_ACCESS_TOKEN = "test-access-token";
2627

27-
@ClassRule
28-
public static JenkinsRule r = new JenkinsRule();
28+
private static JenkinsRule r;
29+
30+
@BeforeAll
31+
static void init(JenkinsRule rule) {
32+
r = rule;
33+
}
2934

30-
@Test(expected = AbortException.class)
31-
public void testGetRobotCredentialsInvalidCredentialsIdAbortException() throws AbortException {
32-
CredentialsUtil.getRobotCredentials(
33-
r.jenkins, ImmutableList.<DomainRequirement>of(), TEST_INVALID_CREDENTIALS_ID);
35+
@Test
36+
void testGetRobotCredentialsInvalidCredentialsIdAbortException() {
37+
assertThrows(
38+
AbortException.class,
39+
() -> CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.of(), TEST_INVALID_CREDENTIALS_ID));
3440
}
3541

36-
@Test(expected = GoogleRobotPrivateKeyCredentials.PrivateKeyNotSetException.class)
37-
public void testGetGoogleCredentialAbortException() throws Exception {
42+
@Test
43+
void testGetGoogleCredentialAbortException() throws Exception {
3844
SecretBytes bytes =
3945
SecretBytes.fromBytes("{\"client_email\": \"[email protected]\"}".getBytes(StandardCharsets.UTF_8));
4046
JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig();
@@ -44,42 +50,52 @@ public void testGetGoogleCredentialAbortException() throws Exception {
4450
new GoogleRobotPrivateKeyCredentials(TEST_INVALID_CREDENTIALS_ID, serviceAccountConfig, null);
4551
CredentialsStore store = new SystemCredentialsProvider.ProviderImpl().getStore(r.jenkins);
4652
store.addCredentials(Domain.global(), robotCreds);
47-
CredentialsUtil.getGoogleCredential(robotCreds);
53+
assertThrows(
54+
GoogleRobotPrivateKeyCredentials.PrivateKeyNotSetException.class,
55+
() -> CredentialsUtil.getGoogleCredential(robotCreds));
4856
}
4957

50-
@Test(expected = NullPointerException.class)
51-
public void testGetRobotCredentialsWithEmptyItemGroup() throws AbortException {
52-
CredentialsUtil.getRobotCredentials(null, ImmutableList.<DomainRequirement>of(), TEST_CREDENTIALS_ID);
58+
@Test
59+
void testGetRobotCredentialsWithEmptyItemGroup() {
60+
assertThrows(
61+
NullPointerException.class,
62+
() -> CredentialsUtil.getRobotCredentials(null, ImmutableList.of(), TEST_CREDENTIALS_ID));
5363
}
5464

55-
@Test(expected = NullPointerException.class)
56-
public void testGetRobotCredentialsWithEmptyDomainRequirements() throws AbortException {
57-
CredentialsUtil.getRobotCredentials(r.jenkins, null, TEST_CREDENTIALS_ID);
65+
@Test
66+
void testGetRobotCredentialsWithEmptyDomainRequirements() {
67+
assertThrows(
68+
NullPointerException.class,
69+
() -> CredentialsUtil.getRobotCredentials(r.jenkins, null, TEST_CREDENTIALS_ID));
5870
}
5971

60-
@Test(expected = IllegalArgumentException.class)
61-
public void testGetRobotCredentialsWithNullCredentialsId() throws AbortException {
62-
CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.<DomainRequirement>of(), null);
72+
@Test
73+
void testGetRobotCredentialsWithNullCredentialsId() {
74+
assertThrows(
75+
IllegalArgumentException.class,
76+
() -> CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.of(), null));
6377
}
6478

65-
@Test(expected = IllegalArgumentException.class)
66-
public void testGetRobotCredentialsWithEmptyCredentialsId() throws AbortException {
67-
CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.<DomainRequirement>of(), "");
79+
@Test
80+
void testGetRobotCredentialsWithEmptyCredentialsId() {
81+
assertThrows(
82+
IllegalArgumentException.class,
83+
() -> CredentialsUtil.getRobotCredentials(r.jenkins, ImmutableList.of(), ""));
6884
}
6985

70-
@Test(expected = IllegalArgumentException.class)
71-
public void testGetAccessTokenWithEmptyCredentialsId() throws IOException {
72-
CredentialsUtil.getAccessToken("");
86+
@Test
87+
void testGetAccessTokenWithEmptyCredentialsId() {
88+
assertThrows(IllegalArgumentException.class, () -> CredentialsUtil.getAccessToken(""));
7389
}
7490

75-
@Test(expected = NullPointerException.class)
76-
public void testGetAccessTokenWithNullItemGroup() throws IOException {
77-
CredentialsUtil.getAccessToken(null, TEST_CREDENTIALS_ID);
91+
@Test
92+
void testGetAccessTokenWithNullItemGroup() {
93+
assertThrows(NullPointerException.class, () -> CredentialsUtil.getAccessToken(null, TEST_CREDENTIALS_ID));
7894
}
7995

80-
@Test(expected = NullPointerException.class)
81-
public void testGetAccessTokenWithNullGoogleCredential() throws IOException {
96+
@Test
97+
void testGetAccessTokenWithNullGoogleCredential() {
8298
Credential googleCredential = null;
83-
CredentialsUtil.getAccessToken(googleCredential);
99+
assertThrows(NullPointerException.class, () -> CredentialsUtil.getAccessToken(googleCredential));
84100
}
85101
}

src/test/java/com/google/jenkins/plugins/k8sengine/ITUtil.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.jenkins.plugins.k8sengine;
1818

19-
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2020

2121
import com.cloudbees.plugins.credentials.SecretBytes;
2222
import com.google.common.io.ByteStreams;
@@ -29,9 +29,9 @@
2929
import java.io.File;
3030
import java.io.IOException;
3131
import java.nio.charset.StandardCharsets;
32+
import java.nio.file.Files;
3233
import java.util.UUID;
3334
import java.util.logging.Logger;
34-
import org.junit.rules.TemporaryFolder;
3535

3636
/** Provides a library of utility functions for integration tests. */
3737
public class ITUtil {
@@ -50,13 +50,12 @@ static String formatRandomName(String prefix) {
5050
* specified {@link Project}.
5151
*
5252
* @param testProject The {@link Project} the test workspace for.
53-
* @return The {@link TemporaryFolder} serving as the test workspace.
53+
* @return The {@link File} serving as the test workspace.
5454
* @throws IOException If an error occurred while creating the test workspace.
5555
*/
56-
static TemporaryFolder createTestWorkspace(Project testProject) throws IOException {
57-
TemporaryFolder testWorkspace = new TemporaryFolder();
58-
testWorkspace.create();
59-
testProject.setCustomWorkspace(testWorkspace.getRoot().toString());
56+
static File createTestWorkspace(Project testProject) throws IOException {
57+
File testWorkspace = Files.createTempDirectory(testProject.getName()).toFile();
58+
testProject.setCustomWorkspace(testWorkspace.toString());
6059
return testWorkspace;
6160
}
6261

@@ -69,7 +68,7 @@ static TemporaryFolder createTestWorkspace(Project testProject) throws IOExcepti
6968
* @throws IOException If an error occurred while copying test file.
7069
* @throws InterruptedException If an error occurred while copying test file.
7170
*/
72-
static void copyTestFileToDir(Class testClass, String toDir, String testFile)
71+
static void copyTestFileToDir(Class<?> testClass, String toDir, String testFile)
7372
throws IOException, InterruptedException {
7473
FilePath dirPath = new FilePath(new File(toDir));
7574
String testFileContents = loadResource(testClass, testFile);
@@ -85,7 +84,7 @@ static void copyTestFileToDir(Class testClass, String toDir, String testFile)
8584
* @return The contents of the loaded resource.
8685
* @throws IOException If an error occurred during loading.
8786
*/
88-
static String loadResource(Class testClass, String name) throws IOException {
87+
static String loadResource(Class<?> testClass, String name) throws IOException {
8988
return new String(ByteStreams.toByteArray(testClass.getResourceAsStream(name)));
9089
}
9190

@@ -114,7 +113,7 @@ static String getLocation() {
114113
if (location == null) {
115114
location = System.getenv("GOOGLE_PROJECT_ZONE");
116115
}
117-
assertNotNull("GOOGLE_PROJECT_LOCATION env var must be set", location);
116+
assertNotNull(location, "GOOGLE_PROJECT_LOCATION env var must be set");
118117
return location;
119118
}
120119

@@ -126,7 +125,7 @@ static String getLocation() {
126125
*/
127126
public static ServiceAccountConfig getServiceAccountConfig() {
128127
String serviceAccountKeyJson = System.getenv("GOOGLE_CREDENTIALS");
129-
assertNotNull("GOOGLE_CREDENTIALS env var must be set", serviceAccountKeyJson);
128+
assertNotNull(serviceAccountKeyJson, "GOOGLE_CREDENTIALS env var must be set");
130129
SecretBytes bytes = SecretBytes.fromBytes(serviceAccountKeyJson.getBytes(StandardCharsets.UTF_8));
131130
JsonServiceAccountConfig config = new JsonServiceAccountConfig();
132131
config.setSecretJsonKey(bytes);

0 commit comments

Comments
 (0)