Skip to content

Commit 6dfc6ff

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 4874991 commit 6dfc6ff

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

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

+45-34
Original file line numberDiff line numberDiff line change
@@ -25,81 +25,94 @@
2525

2626
import com.atlassian.bitbucket.jenkins.internal.config.BitbucketTokenCredentialsImpl;
2727
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
28-
import hudson.Extension;
2928
import hudson.util.HistoricalSecrets;
3029
import io.fabric8.kubernetes.api.model.Secret;
3130
import io.fabric8.kubernetes.client.utils.Serialization;
3231
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;
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;
36+
import org.mockito.MockedStatic;
37+
3938
import java.io.InputStream;
4039
import static org.hamcrest.CoreMatchers.is;
4140
import static org.hamcrest.CoreMatchers.notNullValue;
42-
import static org.junit.Assert.assertThat;
43-
import static org.junit.Assert.fail;
41+
import static org.hamcrest.MatcherAssert.assertThat;
42+
import static org.junit.jupiter.api.Assertions.assertNotNull;
43+
import static org.junit.jupiter.api.Assertions.assertThrows;
44+
import static org.mockito.ArgumentMatchers.anyInt;
45+
import static org.mockito.Mockito.mock;
46+
import static org.mockito.Mockito.mockStatic;
47+
import static org.mockito.Mockito.when;
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()))
68+
.thenAnswer(it -> new byte[(Integer) (it.getArguments()[0])]);
69+
}
70+
71+
@AfterAll
72+
static void resetMockStatic() {
73+
csMockStatic.close();
74+
hsMockStatic.close();
6275
}
6376

6477
@Test
65-
public void canConvert() throws Exception {
78+
void canConvert() {
6679
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
6780
assertThat("correct registration of valid type", converter.canConvert("bitbucketToken"), is(true));
6881
assertThat("incorrect type is rejected", converter.canConvert("something"), is(false));
6982
}
7083

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

7588
try (InputStream is = get("missing-text.yaml")) {
7689
Secret secret = Serialization.unmarshal(is, Secret.class);
77-
assertThat("The Secret was loaded correctly from disk", notNullValue());
90+
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());
7891

79-
BitbucketTokenCredentialsImpl credential = converter.convert(secret);
92+
assertThrows(CredentialsConvertionException.class, () -> converter.convert(secret));
8093
}
8194
}
8295

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

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

91-
BitbucketTokenCredentialsImpl credential = converter.convert(secret);
104+
assertThrows(CredentialsConvertionException.class, () -> converter.convert(secret));
92105
}
93106
}
94107

95108
@Test
96-
public void canConvertAValidSecret() throws Exception {
109+
void canConvertAValidSecret() throws Exception {
97110
ConfidentialStore.get();
98111
BitbucketCredentialConverter converter = new BitbucketCredentialConverter();
99112

100113
try (InputStream is = get("valid.yaml")) {
101114
Secret secret = Serialization.unmarshal(is, Secret.class);
102-
assertThat("The Secret was loaded correctly from disk", notNullValue());
115+
assertThat("The Secret was loaded correctly from disk", secret, notNullValue());
103116

104117
BitbucketTokenCredentialsImpl credential = converter.convert(secret);
105118
assertThat(credential, notNullValue());
@@ -112,9 +125,7 @@ public void canConvertAValidSecret() throws Exception {
112125

113126
private static InputStream get(String resource) {
114127
InputStream is = BitbucketCredentialConverterTest.class.getResourceAsStream(resource);
115-
if (is == null) {
116-
fail("failed to load resource " + resource);
117-
}
128+
assertNotNull(is, "failed to load resource " + resource);
118129
return is;
119130
}
120131
}

0 commit comments

Comments
 (0)