Skip to content

Commit 1c780f6

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 56fa0c0 commit 1c780f6

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

Diff for: src/test/java/ch/jonesbusy/jenkins/openshiftcredentialsk8s/OpenshiftTokenCredentialConverterTest.java

+47-34
Original file line numberDiff line numberDiff line change
@@ -25,71 +25,86 @@
2525

2626
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
2727
import com.openshift.jenkins.plugins.OpenShiftTokenCredentials;
28-
29-
import hudson.Extension;
3028
import hudson.util.HistoricalSecrets;
3129
import io.fabric8.kubernetes.api.model.Secret;
3230
import io.fabric8.kubernetes.client.utils.Serialization;
3331
import jenkins.security.ConfidentialStore;
34-
import org.junit.Before;
35-
import org.junit.BeforeClass;
36-
import org.junit.Test;
32+
import org.junit.jupiter.api.AfterAll;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Test;
3736
import org.mockito.ArgumentMatchers;
37+
import org.mockito.MockedStatic;
3838
import org.mockito.Mockito;
39+
3940
import java.io.InputStream;
41+
4042
import static org.hamcrest.CoreMatchers.is;
4143
import static org.hamcrest.CoreMatchers.notNullValue;
4244
import static org.hamcrest.MatcherAssert.assertThat;
43-
import static org.junit.Assert.fail;
45+
import static org.junit.jupiter.api.Assertions.assertNotNull;
46+
import static org.junit.jupiter.api.Assertions.assertThrows;
4447

4548
/**
4649
* Tests for {@link OpenshiftTokenCredentialConverter}.
4750
*/
48-
@Extension
49-
public class OpenshiftTokenCredentialConverterTest {
51+
class OpenshiftTokenCredentialConverterTest {
52+
53+
private static MockedStatic<ConfidentialStore> confidentialStore;
54+
private static MockedStatic<HistoricalSecrets> historicalSecrets;
5055

51-
@BeforeClass
52-
public static void beforeClass() {
53-
Mockito.mockStatic(ConfidentialStore.class);
54-
Mockito.mockStatic(HistoricalSecrets.class);
56+
@BeforeAll
57+
static void beforeAll() {
58+
confidentialStore = Mockito.mockStatic(ConfidentialStore.class);
59+
historicalSecrets = Mockito.mockStatic(HistoricalSecrets.class);
5560
}
5661

57-
@Before
58-
public void before() {
62+
@AfterAll
63+
static void afterAll() {
64+
confidentialStore.close();
65+
historicalSecrets.close();
66+
}
67+
68+
@BeforeEach
69+
void setUp() {
5970
ConfidentialStore csMock = Mockito.mock(ConfidentialStore.class);
6071
Mockito.when(ConfidentialStore.get()).thenReturn(csMock);
61-
Mockito.when(csMock.randomBytes(ArgumentMatchers.anyInt())).thenAnswer( it -> new byte[ (Integer)(it.getArguments()[0])] );
72+
Mockito.when(csMock.randomBytes(ArgumentMatchers.anyInt())).thenAnswer(it -> new byte[(Integer) (it.getArguments()[0])]);
6273
}
6374

6475
@Test
65-
public void canConvert() throws Exception {
76+
void canConvert() {
6677
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter();
6778
assertThat("correct registration of valid type", converter.canConvert("openshiftToken"), is(true));
6879
assertThat("incorrect type is rejected", converter.canConvert("something"), is(false));
6980
}
7081

71-
@Test(expected = CredentialsConvertionException.class)
72-
public void failsToConvertASecretMissingText() throws Exception {
82+
@Test
83+
void failsToConvertASecretMissingText() {
7384
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter();
74-
try (InputStream is = get("missing-text.yaml")) {
75-
Secret secret = Serialization.unmarshal(is, Secret.class);
76-
assertThat("The Secret was loaded correctly from disk", notNullValue());
77-
converter.convert(secret);
78-
}
85+
assertThrows(CredentialsConvertionException.class, () -> {
86+
try (InputStream is = get("missing-text.yaml")) {
87+
Secret secret = Serialization.unmarshal(is, Secret.class);
88+
assertThat("The Secret was loaded correctly from disk", notNullValue());
89+
converter.convert(secret);
90+
}
91+
});
7992
}
8093

81-
@Test(expected = CredentialsConvertionException.class)
82-
public void failsToConvertWithNonBase64EncodedText() throws Exception {
94+
@Test
95+
void failsToConvertWithNonBase64EncodedText() {
8396
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter();
84-
try (InputStream is = get("text-isnt-base64.yaml")) {
85-
Secret secret = Serialization.unmarshal(is, Secret.class);
86-
assertThat("The Secret was loaded correctly from disk", notNullValue());
87-
converter.convert(secret);
88-
}
97+
assertThrows(CredentialsConvertionException.class, () -> {
98+
try (InputStream is = get("text-isnt-base64.yaml")) {
99+
Secret secret = Serialization.unmarshal(is, Secret.class);
100+
assertThat("The Secret was loaded correctly from disk", notNullValue());
101+
converter.convert(secret);
102+
}
103+
});
89104
}
90105

91106
@Test
92-
public void canConvertAValidSecret() throws Exception {
107+
void canConvertAValidSecret() throws Exception {
93108
ConfidentialStore.get();
94109
OpenshiftTokenCredentialConverter converter = new OpenshiftTokenCredentialConverter();
95110
try (InputStream is = get("valid.yaml")) {
@@ -107,9 +122,7 @@ public void canConvertAValidSecret() throws Exception {
107122

108123
private static InputStream get(String resource) {
109124
InputStream is = OpenshiftTokenCredentialConverterTest.class.getResourceAsStream(resource);
110-
if (is == null) {
111-
fail("failed to load resource " + resource);
112-
}
125+
assertNotNull(is, "failed to load resource " + resource);
113126
return is;
114127
}
115128
}

0 commit comments

Comments
 (0)