Skip to content

Commit cccaa44

Browse files
authored
refactor: migration tests in appengine module from JUnit4 to JUnit5 (googleapis#756) (googleapis#760)
1 parent 5673678 commit cccaa44

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131

3232
package com.google.auth.appengine;
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.assertNotNull;
38-
import static org.junit.Assert.assertNotSame;
39-
import static org.junit.Assert.assertTrue;
40-
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.assertNotNull;
38+
import static org.junit.jupiter.api.Assertions.assertNotSame;
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
40+
import static org.junit.jupiter.api.Assertions.assertTrue;
4141

4242
import com.google.auth.Credentials;
4343
import com.google.auth.oauth2.AccessToken;
@@ -51,21 +51,18 @@
5151
import java.util.Date;
5252
import java.util.List;
5353
import java.util.Map;
54-
import org.junit.Test;
55-
import org.junit.runner.RunWith;
56-
import org.junit.runners.JUnit4;
54+
import org.junit.jupiter.api.Test;
5755

5856
/** Unit tests for AppEngineCredentials */
59-
@RunWith(JUnit4.class)
60-
public class AppEngineCredentialsTest extends BaseSerializationTest {
57+
class AppEngineCredentialsTest extends BaseSerializationTest {
6158

6259
private static Collection<String> SCOPES =
6360
Collections.unmodifiableCollection(Arrays.asList("scope1", "scope2"));
6461
private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo");
6562
private static final String EXPECTED_ACCOUNT = "serviceAccount";
6663

6764
@Test
68-
public void constructor_usesAppIdentityService() throws IOException {
65+
void constructor_usesAppIdentityService() throws IOException {
6966
String expectedAccessToken = "ExpectedAccessToken";
7067

7168
MockAppIdentityService appIdentity = new MockAppIdentityService();
@@ -83,7 +80,7 @@ public void constructor_usesAppIdentityService() throws IOException {
8380
}
8481

8582
@Test
86-
public void refreshAccessToken_sameAs() throws IOException {
83+
void refreshAccessToken_sameAs() throws IOException {
8784
String expectedAccessToken = "ExpectedAccessToken";
8885

8986
MockAppIdentityService appIdentity = new MockAppIdentityService();
@@ -100,7 +97,7 @@ public void refreshAccessToken_sameAs() throws IOException {
10097
}
10198

10299
@Test
103-
public void getAccount_sameAs() throws IOException {
100+
void getAccount_sameAs() {
104101
MockAppIdentityService appIdentity = new MockAppIdentityService();
105102
appIdentity.setServiceAccountName(EXPECTED_ACCOUNT);
106103
AppEngineCredentials credentials =
@@ -112,7 +109,7 @@ public void getAccount_sameAs() throws IOException {
112109
}
113110

114111
@Test
115-
public void sign_sameAs() throws IOException {
112+
void sign_sameAs() {
116113
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
117114
MockAppIdentityService appIdentity = new MockAppIdentityService();
118115
appIdentity.setSignature(expectedSignature);
@@ -125,7 +122,7 @@ public void sign_sameAs() throws IOException {
125122
}
126123

127124
@Test
128-
public void createScoped_clonesWithScopes() throws IOException {
125+
void createScoped_clonesWithScopes() throws IOException {
129126
String expectedAccessToken = "ExpectedAccessToken";
130127
Collection<String> emptyScopes = Collections.emptyList();
131128

@@ -138,11 +135,10 @@ public void createScoped_clonesWithScopes() throws IOException {
138135
.setAppIdentityService(appIdentity)
139136
.build();
140137
assertTrue(credentials.createScopedRequired());
141-
try {
142-
credentials.getRequestMetadata(CALL_URI);
143-
fail("Should not be able to use credential without scopes.");
144-
} catch (Exception expected) {
145-
}
138+
assertThrows(
139+
Exception.class,
140+
() -> credentials.getRequestMetadata(CALL_URI),
141+
"Should not be able to use credential without scopes.");
146142
assertEquals(0, appIdentity.getGetAccessTokenCallCount());
147143

148144
GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
@@ -155,7 +151,7 @@ public void createScoped_clonesWithScopes() throws IOException {
155151
}
156152

157153
@Test
158-
public void equals_true() throws IOException {
154+
void equals_true() {
159155
Collection<String> emptyScopes = Collections.emptyList();
160156
MockAppIdentityService appIdentity = new MockAppIdentityService();
161157

@@ -175,7 +171,7 @@ public void equals_true() throws IOException {
175171
}
176172

177173
@Test
178-
public void equals_false_scopes() throws IOException {
174+
void equals_false_scopes() {
179175
Collection<String> emptyScopes = Collections.emptyList();
180176
Collection<String> scopes = Collections.singleton("SomeScope");
181177
MockAppIdentityService appIdentity = new MockAppIdentityService();
@@ -195,7 +191,7 @@ public void equals_false_scopes() throws IOException {
195191
}
196192

197193
@Test
198-
public void toString_containsFields() throws IOException {
194+
void toString_containsFields() {
199195
String expectedToString =
200196
String.format(
201197
"AppEngineCredentials{scopes=[%s], scopesRequired=%b, appIdentityServiceClassName=%s}",
@@ -213,7 +209,7 @@ public void toString_containsFields() throws IOException {
213209
}
214210

215211
@Test
216-
public void hashCode_equals() throws IOException {
212+
void hashCode_equals() {
217213
Collection<String> emptyScopes = Collections.emptyList();
218214
MockAppIdentityService appIdentity = new MockAppIdentityService();
219215
AppEngineCredentials credentials =
@@ -230,7 +226,7 @@ public void hashCode_equals() throws IOException {
230226
}
231227

232228
@Test
233-
public void serialize() throws IOException, ClassNotFoundException {
229+
void serialize() throws IOException, ClassNotFoundException {
234230
Collection<String> scopes = Collections.singleton("SomeScope");
235231
MockAppIdentityService appIdentity = new MockAppIdentityService();
236232
AppEngineCredentials credentials =
@@ -249,14 +245,7 @@ private static void assertContainsBearerToken(Map<String, List<String>> metadata
249245
assertNotNull(token);
250246
String expectedValue = "Bearer " + token;
251247
List<String> authorizations = metadata.get("Authorization");
252-
assertNotNull("Authorization headers not found", authorizations);
253-
boolean found = false;
254-
for (String authorization : authorizations) {
255-
if (expectedValue.equals(authorization)) {
256-
found = true;
257-
break;
258-
}
259-
}
260-
assertTrue("Bearer token not found", found);
248+
assertNotNull(authorizations, "Authorization headers not found");
249+
assertTrue(authorizations.contains(expectedValue), "Bearer token not found");
261250
}
262251
}

appengine/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
<artifactId>guava</artifactId>
6969
</dependency>
7070
<dependency>
71-
<groupId>junit</groupId>
72-
<artifactId>junit</artifactId>
71+
<groupId>org.junit.jupiter</groupId>
72+
<artifactId>junit-jupiter-api</artifactId>
7373
<scope>test</scope>
7474
</dependency>
7575
<dependency>

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
6262
<project.google.http.version>1.40.1</project.google.http.version>
6363
<project.junit.version>4.13.2</project.junit.version>
64+
<project.junit.bom.version>5.8.1</project.junit.bom.version>
6465
<project.guava.version>31.0.1-android</project.guava.version>
6566
<project.appengine.version>1.9.91</project.appengine.version>
6667
<project.findbugs.version>3.0.2</project.findbugs.version>
@@ -71,6 +72,13 @@
7172

7273
<dependencyManagement>
7374
<dependencies>
75+
<dependency>
76+
<groupId>org.junit</groupId>
77+
<artifactId>junit-bom</artifactId>
78+
<version>${project.junit.bom.version}</version>
79+
<type>pom</type>
80+
<scope>import</scope>
81+
</dependency>
7482
<dependency>
7583
<groupId>com.google.auth</groupId>
7684
<artifactId>google-auth-library-credentials</artifactId>

0 commit comments

Comments
 (0)