Skip to content

Commit 4e6576f

Browse files
authored
chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - first iteration (googleapis#756) (googleapis#762)
* chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - first iteration (googleapis#756) * chore: expand imports (googleapis#756)
1 parent a64d35c commit 4e6576f

11 files changed

+428
-478
lines changed

oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
package com.google.auth.http;
3333

3434
import static org.hamcrest.CoreMatchers.instanceOf;
35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertThat;
35+
import static org.hamcrest.MatcherAssert.assertThat;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
3737

3838
import com.google.api.client.http.GenericUrl;
3939
import com.google.api.client.http.HttpHeaders;
@@ -47,20 +47,17 @@
4747
import com.google.auth.oauth2.OAuth2Credentials;
4848
import com.google.auth.oauth2.UserCredentials;
4949
import java.io.IOException;
50-
import org.junit.Test;
51-
import org.junit.runner.RunWith;
52-
import org.junit.runners.JUnit4;
50+
import org.junit.jupiter.api.Test;
5351

5452
/** Test case for {@link HttpCredentialsAdapter}. */
55-
@RunWith(JUnit4.class)
56-
public class HttpCredentialsAdapterTest {
53+
class HttpCredentialsAdapterTest {
5754

5855
private static final String CLIENT_SECRET = "jakuaL9YyieakhECKL2SwZcu";
5956
private static final String CLIENT_ID = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws";
6057
private static final String REFRESH_TOKEN = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
6158

6259
@Test
63-
public void initialize_populatesOAuth2Credentials() throws IOException {
60+
void initialize_populatesOAuth2Credentials() throws IOException {
6461
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
6562
final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
6663
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
@@ -87,7 +84,7 @@ public void initialize_populatesOAuth2Credentials() throws IOException {
8784
}
8885

8986
@Test
90-
public void initialize_populatesOAuth2Credentials_handle401() throws IOException {
87+
void initialize_populatesOAuth2Credentials_handle401() throws IOException {
9188
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
9289
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
9390

@@ -125,7 +122,7 @@ public void initialize_populatesOAuth2Credentials_handle401() throws IOException
125122
}
126123

127124
@Test
128-
public void initialize_noURI() throws IOException {
125+
void initialize_noURI() throws IOException {
129126
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
130127
final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
131128
MockTokenServerTransportFactory tokenServerTransportFactory =
@@ -154,7 +151,7 @@ public void initialize_noURI() throws IOException {
154151
}
155152

156153
@Test
157-
public void getCredentials() {
154+
void getCredentials() {
158155
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
159156
MockTokenServerTransportFactory tokenServerTransportFactory =
160157
new MockTokenServerTransportFactory();

oauth2_http/javatests/com/google/auth/oauth2/AccessTokenTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,54 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.assertFalse;
36-
import static org.junit.Assert.assertTrue;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
3737

3838
import java.io.IOException;
3939
import java.util.Date;
40-
import org.junit.Test;
41-
import org.junit.runner.RunWith;
42-
import org.junit.runners.JUnit4;
40+
import org.junit.jupiter.api.Test;
4341

4442
/** Unit tests for AccessToken */
45-
@RunWith(JUnit4.class)
46-
public class AccessTokenTest extends BaseSerializationTest {
43+
class AccessTokenTest extends BaseSerializationTest {
4744

4845
private static final String TOKEN = "AccessToken";
4946
private static final Date EXPIRATION_DATE = new Date();
5047

5148
@Test
52-
public void constructor() {
49+
void constructor() {
5350
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
5451
assertEquals(TOKEN, accessToken.getTokenValue());
5552
assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
5653
assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
5754
}
5855

5956
@Test
60-
public void equals_true() throws IOException {
57+
void equals_true() {
6158
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
6259
AccessToken otherAccessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
6360
assertTrue(accessToken.equals(otherAccessToken));
6461
assertTrue(otherAccessToken.equals(accessToken));
6562
}
6663

6764
@Test
68-
public void equals_false_token() throws IOException {
65+
void equals_false_token() {
6966
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
7067
AccessToken otherAccessToken = new AccessToken("otherToken", EXPIRATION_DATE);
7168
assertFalse(accessToken.equals(otherAccessToken));
7269
assertFalse(otherAccessToken.equals(accessToken));
7370
}
7471

7572
@Test
76-
public void equals_false_expirationDate() throws IOException {
73+
void equals_false_expirationDate() {
7774
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
7875
AccessToken otherAccessToken = new AccessToken(TOKEN, new Date(EXPIRATION_DATE.getTime() + 42));
7976
assertFalse(accessToken.equals(otherAccessToken));
8077
assertFalse(otherAccessToken.equals(accessToken));
8178
}
8279

8380
@Test
84-
public void toString_containsFields() {
81+
void toString_containsFields() {
8582
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
8683
String expectedToString =
8784
String.format(
@@ -91,14 +88,14 @@ public void toString_containsFields() {
9188
}
9289

9390
@Test
94-
public void hashCode_equals() throws IOException {
91+
void hashCode_equals() throws IOException {
9592
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
9693
AccessToken otherAccessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
9794
assertEquals(accessToken.hashCode(), otherAccessToken.hashCode());
9895
}
9996

10097
@Test
101-
public void serialize() throws IOException, ClassNotFoundException {
98+
void serialize() throws IOException, ClassNotFoundException {
10299
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
103100
AccessToken deserializedAccessToken = serializeAndDeserialize(accessToken);
104101
assertEquals(accessToken, deserializedAccessToken);

oauth2_http/javatests/com/google/auth/oauth2/AppEngineCredentialsTest.java

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static org.junit.Assert.assertArrayEquals;
35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertFalse;
37-
import static org.junit.Assert.assertNotSame;
38-
import static org.junit.Assert.assertTrue;
39-
import static org.junit.Assert.fail;
34+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertNotSame;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
4040

4141
import com.google.common.collect.ImmutableMap;
4242
import java.io.IOException;
@@ -47,12 +47,9 @@
4747
import java.util.Date;
4848
import java.util.List;
4949
import java.util.Map;
50-
import org.junit.Test;
51-
import org.junit.runner.RunWith;
52-
import org.junit.runners.JUnit4;
50+
import org.junit.jupiter.api.Test;
5351

54-
@RunWith(JUnit4.class)
55-
public class AppEngineCredentialsTest extends BaseSerializationTest {
52+
class AppEngineCredentialsTest extends BaseSerializationTest {
5653

5754
private static final String EXPECTED_ACCESS_TOKEN = "ExpectedAccessToken";
5855
private static final Date EXPECTED_EXPIRATION_DATE =
@@ -66,7 +63,7 @@ public class AppEngineCredentialsTest extends BaseSerializationTest {
6663
Collections.unmodifiableCollection(Arrays.asList("scope3"));
6764

6865
@Test
69-
public void constructor_usesAppIdentityService() throws IOException {
66+
void constructor_usesAppIdentityService() throws IOException {
7067
Collection<String> scopes = Collections.singleton("SomeScope");
7168
TestAppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
7269
List<String> forNameArgs = credentials.getForNameArgs();
@@ -78,51 +75,50 @@ public void constructor_usesAppIdentityService() throws IOException {
7875
}
7976

8077
@Test
81-
public void constructor_noAppEngineRuntime_throwsHelpfulLoadError() throws IOException {
82-
try {
83-
new TestAppEngineCredentialsNoSdk();
84-
fail("Credential expected to fail to load if credential class not present.");
85-
} catch (IOException e) {
86-
String message = e.getMessage();
87-
assertTrue(message.contains("Check that the App Engine SDK is deployed."));
88-
assertTrue(e.getCause() instanceof ClassNotFoundException);
89-
assertTrue(
90-
e.getCause()
91-
.getMessage()
92-
.contains(AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS));
93-
}
78+
void constructor_noAppEngineRuntime_throwsHelpfulLoadError() {
79+
IOException exception =
80+
assertThrows(
81+
IOException.class,
82+
TestAppEngineCredentialsNoSdk::new,
83+
"Credential expected to fail to load if credential class not present.");
84+
String message = exception.getMessage();
85+
assertTrue(message.contains("Check that the App Engine SDK is deployed."));
86+
assertTrue(exception.getCause() instanceof ClassNotFoundException);
87+
assertTrue(
88+
exception
89+
.getCause()
90+
.getMessage()
91+
.contains(AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS));
9492
}
9593

9694
@Test
97-
public void refreshAccessToken_sameAs() throws IOException {
95+
void refreshAccessToken_sameAs() throws IOException {
9896
TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
9997
AccessToken accessToken = credentials.refreshAccessToken();
10098
assertEquals(EXPECTED_ACCESS_TOKEN, accessToken.getTokenValue());
10199
assertEquals(EXPECTED_EXPIRATION_DATE, accessToken.getExpirationTime());
102100
}
103101

104102
@Test
105-
public void getAccount_sameAs() throws IOException {
103+
void getAccount_sameAs() throws IOException {
106104
TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
107105
assertEquals(EXPECTED_ACCOUNT, credentials.getAccount());
108106
}
109107

110108
@Test
111-
public void sign_sameAs() throws IOException {
109+
void sign_sameAs() throws IOException {
112110
TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
113111
assertArrayEquals(EXPECTED_SIGNATURE, credentials.sign("Bytes to sign".getBytes()));
114112
}
115113

116114
@Test
117-
public void createScoped_clonesWithScopes() throws IOException {
115+
void createScoped_clonesWithScopes() throws IOException {
118116
TestAppEngineCredentials credentials = new TestAppEngineCredentials(null);
119117
assertTrue(credentials.createScopedRequired());
120-
try {
121-
credentials.refreshAccessToken();
122-
fail("Should not be able to use credential without scopes.");
123-
} catch (Exception expected) {
124-
// Expected
125-
}
118+
assertThrows(
119+
Exception.class,
120+
credentials::refreshAccessToken,
121+
"Should not be able to use credential without scopes.");
126122

127123
GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
128124
assertNotSame(credentials, scopedCredentials);
@@ -133,7 +129,7 @@ public void createScoped_clonesWithScopes() throws IOException {
133129
}
134130

135131
@Test
136-
public void createScoped_defaultScopes() throws IOException {
132+
void createScoped_defaultScopes() throws IOException {
137133
TestAppEngineCredentials credentials = new TestAppEngineCredentials(null);
138134
assertTrue(credentials.createScopedRequired());
139135

@@ -152,7 +148,7 @@ public void createScoped_defaultScopes() throws IOException {
152148
}
153149

154150
@Test
155-
public void equals_true() throws IOException {
151+
void equals_true() throws IOException {
156152
GoogleCredentials credentials = new TestAppEngineCredentials(SCOPES);
157153
GoogleCredentials otherCredentials = new TestAppEngineCredentials(SCOPES);
158154
assertTrue(credentials.equals(credentials));
@@ -161,7 +157,7 @@ public void equals_true() throws IOException {
161157
}
162158

163159
@Test
164-
public void equals_false_scopes() throws IOException {
160+
void equals_false_scopes() throws IOException {
165161
final Collection<String> emptyScopes = Collections.emptyList();
166162
Collection<String> scopes = Collections.singleton("SomeScope");
167163
AppEngineCredentials credentials = new TestAppEngineCredentials(emptyScopes);
@@ -171,7 +167,7 @@ public void equals_false_scopes() throws IOException {
171167
}
172168

173169
@Test
174-
public void toString_containsFields() throws IOException {
170+
void toString_containsFields() throws IOException {
175171
String expectedToString =
176172
String.format(
177173
"TestAppEngineCredentials{scopes=[%s], scopesRequired=%b}", "SomeScope", false);
@@ -181,13 +177,13 @@ public void toString_containsFields() throws IOException {
181177
}
182178

183179
@Test
184-
public void hashCode_equals() throws IOException {
180+
void hashCode_equals() throws IOException {
185181
AppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
186182
assertEquals(credentials.hashCode(), credentials.hashCode());
187183
}
188184

189185
@Test
190-
public void serialize() throws IOException, ClassNotFoundException {
186+
void serialize() throws IOException, ClassNotFoundException {
191187
Collection<String> scopes = Collections.singleton("SomeScope");
192188
AppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
193189
GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials);

0 commit comments

Comments
 (0)