Skip to content

Commit 67089f6

Browse files
authored
Merge pull request #271 from strangelookingnerd/migrate_to_junit5
Migrate tests to JUnit5
2 parents 4874991 + 0bcb24f commit 67089f6

File tree

1 file changed

+56
-40
lines changed

1 file changed

+56
-40
lines changed

Diff for: src/test/java/co/johnrowley/jenkins/bitbucketcredentialsk8s/BitbucketCredentialConverterTest.java

+56-40
Original file line numberDiff line numberDiff line change
@@ -23,98 +23,114 @@
2323
*/
2424
package co.johnrowley.jenkins.bitbucketcredentialsk8s;
2525

26+
import static org.hamcrest.CoreMatchers.is;
27+
import static org.hamcrest.CoreMatchers.notNullValue;
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
31+
import static org.mockito.ArgumentMatchers.anyInt;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.mockStatic;
34+
import static org.mockito.Mockito.when;
35+
2636
import com.atlassian.bitbucket.jenkins.internal.config.BitbucketTokenCredentialsImpl;
2737
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
28-
import hudson.Extension;
2938
import hudson.util.HistoricalSecrets;
3039
import io.fabric8.kubernetes.api.model.Secret;
3140
import io.fabric8.kubernetes.client.utils.Serialization;
32-
import jenkins.security.ConfidentialStore;
33-
import org.junit.BeforeClass;
34-
import org.junit.Before;
35-
import org.junit.Test;
36-
import org.junit.runner.RunWith;
37-
import org.mockito.ArgumentMatchers;
38-
import org.mockito.Mockito;
3941
import java.io.InputStream;
40-
import static org.hamcrest.CoreMatchers.is;
41-
import static org.hamcrest.CoreMatchers.notNullValue;
42-
import static org.junit.Assert.assertThat;
43-
import static org.junit.Assert.fail;
42+
import jenkins.security.ConfidentialStore;
43+
import org.junit.jupiter.api.AfterAll;
44+
import org.junit.jupiter.api.BeforeAll;
45+
import org.junit.jupiter.api.BeforeEach;
46+
import org.junit.jupiter.api.Test;
47+
import org.mockito.MockedStatic;
4448

4549
/**
4650
* Tests for {@link BitbucketCredentialConverter}.
4751
*/
48-
@Extension
49-
public class BitbucketCredentialConverterTest {
52+
class BitbucketCredentialConverterTest {
53+
54+
private static MockedStatic<ConfidentialStore> csMockStatic;
55+
private static MockedStatic<HistoricalSecrets> hsMockStatic;
5056

51-
@BeforeClass
52-
public static void mockConfidentialStore() {
53-
Mockito.mockStatic(ConfidentialStore.class);
54-
Mockito.mockStatic(HistoricalSecrets.class);
57+
@BeforeAll
58+
static void mockConfidentialStore() {
59+
csMockStatic = mockStatic(ConfidentialStore.class);
60+
hsMockStatic = mockStatic(HistoricalSecrets.class);
5561
}
5662

57-
@Before
58-
public void before() {
59-
ConfidentialStore csMock = Mockito.mock(ConfidentialStore.class);
60-
Mockito.when(ConfidentialStore.get()).thenReturn(csMock);
61-
Mockito.when(csMock.randomBytes(ArgumentMatchers.anyInt())).thenAnswer( it -> new byte[ (Integer)(it.getArguments()[0])] );
63+
@BeforeEach
64+
void before() {
65+
ConfidentialStore csMock = mock(ConfidentialStore.class);
66+
when(ConfidentialStore.get()).thenReturn(csMock);
67+
when(csMock.randomBytes(anyInt())).thenAnswer(it -> new byte[(Integer) (it.getArguments()[0])]);
68+
}
69+
70+
@AfterAll
71+
static void resetMockStatic() {
72+
csMockStatic.close();
73+
hsMockStatic.close();
6274
}
6375

6476
@Test
65-
public void canConvert() throws Exception {
77+
void canConvert() {
6678
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
6779
assertThat("correct registration of valid type", converter.canConvert("bitbucketToken"), is(true));
6880
assertThat("incorrect type is rejected", converter.canConvert("something"), is(false));
6981
}
7082

71-
@Test(expected = CredentialsConvertionException.class)
72-
public void failsToConvertASecretMissingText() throws Exception {
83+
@Test
84+
void failsToConvertASecretMissingText() throws Exception {
7385
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
7486

7587
try (InputStream is = get("missing-text.yaml")) {
7688
Secret secret = Serialization.unmarshal(is, Secret.class);
77-
assertThat("The Secret was loaded correctly from disk", notNullValue());
89+
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());
7890

79-
BitbucketTokenCredentialsImpl credential = converter.convert(secret);
91+
assertThrows(CredentialsConvertionException.class, () -> converter.convert(secret));
8092
}
8193
}
8294

83-
@Test(expected = CredentialsConvertionException.class)
84-
public void failsToConvertWithNonBase64EncodedText() throws Exception {
95+
@Test
96+
void failsToConvertWithNonBase64EncodedText() throws Exception {
8597
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
8698

8799
try (InputStream is = get("text-isnt-base64.yaml")) {
88100
Secret secret = Serialization.unmarshal(is, Secret.class);
89-
assertThat("The Secret was loaded correctly from disk", notNullValue());
101+
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());
90102

91-
BitbucketTokenCredentialsImpl credential = converter.convert(secret);
103+
assertThrows(CredentialsConvertionException.class, () -> converter.convert(secret));
92104
}
93105
}
94106

95107
@Test
96-
public void canConvertAValidSecret() throws Exception {
108+
void canConvertAValidSecret() throws Exception {
97109
ConfidentialStore.get();
98110
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
99111

100112
try (InputStream is = get("valid.yaml")) {
101113
Secret secret = Serialization.unmarshal(is, Secret.class);
102-
assertThat("The Secret was loaded correctly from disk", notNullValue());
114+
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());
103115

104116
BitbucketTokenCredentialsImpl credential = converter.convert(secret);
105117
assertThat(credential, notNullValue());
106118

107119
assertThat("credential id is mapped correctly", credential.getId(), is("a-test-secret"));
108-
assertThat("credential description is mapped correctly", credential.getDescription(), is("secret bitbucket personal token credential from Kubernetes"));
109-
assertThat("credential text mapped to the secret", credential.getSecret().getPlainText(), is("someSuperDuperSecret"));
120+
assertThat(
121+
"credential description is mapped correctly",
122+
credential.getDescription(),
123+
is("secret bitbucket personal token credential from Kubernetes"));
124+
assertThat(
125+
"credential text mapped to the secret",
126+
credential.getSecret().getPlainText(),
127+
is("someSuperDuperSecret"));
110128
}
111129
}
112130

113131
private static InputStream get(String resource) {
114132
InputStream is = BitbucketCredentialConverterTest.class.getResourceAsStream(resource);
115-
if (is == null) {
116-
fail("failed to load resource " + resource);
117-
}
133+
assertNotNull(is, "failed to load resource " + resource);
118134
return is;
119135
}
120-
}
136+
}

0 commit comments

Comments
 (0)