Skip to content

Commit fd91c65

Browse files
zhumin8burkedavisonlqiu96
committed
chore: Migrate Junit 4 to Junit 5 for showcase (#2757)
fixes #2728, attempt to remove Junit 4 support after migration. Other than POM dependency migrate, changes include: - package name changes - Junit 5 syntax upgrades, e.g. `@Before` --> `@BeforeEach`, Replace assertion methods - remove public modifier on tests and test classes. - Refactor JUnit 4 TemporaryFolder `@Rule` in [ITGdch.java](https://github.com/googleapis/sdk-platform-java/pull/2757/files#diff-6ae7755a0b038e1a2febae2d27e36c762f6751b8c7db577421667069399884b4) to JUnit 5 `@TempDir` - Replace `@Test(timeout = 15000L)` in [ITClientShutdown.java](https://github.com/googleapis/sdk-platform-java/pull/2757/files#diff-70d1df57471178a7a63302f82e4a4855ffbbd642ea67d92d501bd1f7008957ca) with `@Timeout(15)` - Update `@RunWith(Parameterized.class)` test in [ITHttpAnnotation.java](https://github.com/googleapis/sdk-platform-java/pull/2757/files#diff-03d420650ecc9fe78ad4887761043c4fdceaa978f464ce30cfc4ed5f8be9b64d) to `@ParameterizedTest` with `@MethodSource("data")` ~~Note: #2737 creates a new test class with JUnit4 syntax. Depending on merging order, I will either update in this pr, or #2737.~~ Updated. Due to truth library depending on junit 4 ([see issue](google/truth#333)), junit 4 cannot be completely removed, or will encounter `java.lang.ClassNotFoundException: org.junit.runner.notification.RunListener` running tests with maven surefire. To keep things cleaner, excluding the implicitly junit brought in from truth and `junit-vintage-engine`. We could also do the reverse, and make a comment if that's prefered. --------- Co-authored-by: Burke Davison <[email protected]> Co-authored-by: Lawrence Qiu <[email protected]>
1 parent cda8a9d commit fd91c65

21 files changed

+386
-351
lines changed

showcase/gapic-showcase/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,30 @@
132132

133133
<!-- Test dependencies -->
134134
<dependency>
135-
<groupId>junit</groupId>
136-
<artifactId>junit</artifactId>
135+
<groupId>org.junit.jupiter</groupId>
136+
<artifactId>junit-jupiter-engine</artifactId>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>org.junit.vintage</groupId>
141+
<artifactId>junit-vintage-engine</artifactId>
142+
<scope>test</scope>
143+
</dependency>
144+
<dependency>
145+
<groupId>org.junit.jupiter</groupId>
146+
<artifactId>junit-jupiter-params</artifactId>
137147
<scope>test</scope>
138148
</dependency>
139149
<dependency>
140150
<groupId>com.google.truth</groupId>
141151
<artifactId>truth</artifactId>
142152
<version>1.4.2</version>
153+
<exclusions>
154+
<exclusion>
155+
<groupId>junit</groupId>
156+
<artifactId>junit</artifactId>
157+
</exclusion>
158+
</exclusions>
143159
<scope>test</scope>
144160
</dependency>
145161

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.google.showcase.v1beta1.it;
1717

1818
import static com.google.common.truth.Truth.assertThat;
19-
import static org.junit.Assert.assertThrows;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

2121
import com.google.api.gax.httpjson.*;
2222
import com.google.api.gax.rpc.ApiClientHeaderProvider;
@@ -31,15 +31,15 @@
3131
import java.io.IOException;
3232
import java.util.ArrayList;
3333
import java.util.concurrent.TimeUnit;
34-
import org.junit.After;
35-
import org.junit.Before;
36-
import org.junit.Test;
34+
import org.junit.jupiter.api.AfterAll;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
3737

3838
// TODO: add testing on error responses once feat is implemented in showcase.
3939
// https://github.com/googleapis/gapic-showcase/pull/1456
4040
// TODO: watch for showcase gRPC trailer changes suggested in
4141
// https://github.com/googleapis/gapic-showcase/pull/1509#issuecomment-2089147103
42-
public class ITApiVersionHeaders {
42+
class ITApiVersionHeaders {
4343
private static final String HTTP_RESPONSE_HEADER_STRING =
4444
"x-showcase-request-" + ApiClientHeaderProvider.API_VERSION_HEADER_KEY;
4545
private static final Metadata.Key<String> API_VERSION_HEADER_KEY =
@@ -51,7 +51,6 @@ public class ITApiVersionHeaders {
5151
private static final String EXPECTED_EXCEPTION_MESSAGE =
5252
"Header provider can't override the header: "
5353
+ ApiClientHeaderProvider.API_VERSION_HEADER_KEY;
54-
private static final int DEFAULT_AWAIT_TERMINATION_SEC = 10;
5554

5655
// Implement a client interceptor to retrieve the trailing metadata from response.
5756
private static class GrpcCapturingClientInterceptor implements ClientInterceptor {
@@ -149,17 +148,17 @@ public void onClose(int statusCode, HttpJsonMetadata trailers) {
149148
}
150149
}
151150

152-
private HttpJsonCapturingClientInterceptor httpJsonInterceptor;
153-
private GrpcCapturingClientInterceptor grpcInterceptor;
154-
private HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
155-
private GrpcCapturingClientInterceptor grpcComplianceInterceptor;
156-
private EchoClient grpcClient;
157-
private EchoClient httpJsonClient;
158-
private ComplianceClient grpcComplianceClient;
159-
private ComplianceClient httpJsonComplianceClient;
160-
161-
@Before
162-
public void createClients() throws Exception {
151+
private static HttpJsonCapturingClientInterceptor httpJsonInterceptor;
152+
private static GrpcCapturingClientInterceptor grpcInterceptor;
153+
private static HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
154+
private static GrpcCapturingClientInterceptor grpcComplianceInterceptor;
155+
private static EchoClient grpcClient;
156+
private static EchoClient httpJsonClient;
157+
private static ComplianceClient grpcComplianceClient;
158+
private static ComplianceClient httpJsonComplianceClient;
159+
160+
@BeforeAll
161+
static void createClients() throws Exception {
163162
// Create gRPC Interceptor and Client
164163
grpcInterceptor = new GrpcCapturingClientInterceptor();
165164
grpcClient = TestClientInitializer.createGrpcEchoClient(ImmutableList.of(grpcInterceptor));
@@ -183,28 +182,31 @@ public void createClients() throws Exception {
183182
ImmutableList.of(httpJsonComplianceInterceptor));
184183
}
185184

186-
@After
187-
public void destroyClient() throws InterruptedException {
185+
@AfterAll
186+
static void destroyClient() throws InterruptedException {
188187
grpcClient.close();
189188
httpJsonClient.close();
190189
grpcComplianceClient.close();
191190
httpJsonComplianceClient.close();
192191

193-
grpcClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
194-
httpJsonClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
195-
grpcComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
196-
httpJsonComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
192+
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
193+
httpJsonClient.awaitTermination(
194+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
195+
grpcComplianceClient.awaitTermination(
196+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
197+
httpJsonComplianceClient.awaitTermination(
198+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
197199
}
198200

199201
@Test
200-
public void testGrpc_matchesApiVersion() {
202+
void testGrpc_matchesApiVersion() {
201203
grpcClient.echo(EchoRequest.newBuilder().build());
202204
String headerValue = grpcInterceptor.metadata.get(API_VERSION_HEADER_KEY);
203205
assertThat(headerValue).isEqualTo(EXPECTED_ECHO_API_VERSION);
204206
}
205207

206208
@Test
207-
public void testHttpJson_matchesHeaderName() {
209+
void testHttpJson_matchesHeaderName() {
208210
httpJsonClient.echo(EchoRequest.newBuilder().build());
209211
ArrayList headerValues =
210212
(ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
@@ -213,15 +215,15 @@ public void testHttpJson_matchesHeaderName() {
213215
}
214216

215217
@Test
216-
public void testGrpc_noApiVersion() {
218+
void testGrpc_noApiVersion() {
217219
RepeatRequest request =
218220
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
219221
grpcComplianceClient.repeatDataSimplePath(request);
220222
assertThat(API_VERSION_HEADER_KEY).isNotIn(grpcComplianceInterceptor.metadata.keys());
221223
}
222224

223225
@Test
224-
public void testHttpJson_noApiVersion() {
226+
void testHttpJson_noApiVersion() {
225227
RepeatRequest request =
226228
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
227229
httpJsonComplianceClient.repeatDataSimplePath(request);
@@ -230,7 +232,7 @@ public void testHttpJson_noApiVersion() {
230232
}
231233

232234
@Test
233-
public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
235+
void testGrpcEcho_userApiVersionThrowsException() throws IOException {
234236
StubSettings stubSettings =
235237
grpcClient
236238
.getSettings()
@@ -249,7 +251,7 @@ public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
249251
}
250252

251253
@Test
252-
public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
254+
void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
253255
StubSettings stubSettings =
254256
httpJsonClient
255257
.getSettings()
@@ -268,7 +270,7 @@ public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException
268270
}
269271

270272
@Test
271-
public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
273+
void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
272274
StubSettings stubSettingsWithApiVersionHeader =
273275
grpcComplianceClient
274276
.getSettings()
@@ -293,7 +295,7 @@ public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
293295
}
294296

295297
@Test
296-
public void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {
298+
void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {
297299
StubSettings httpJsonStubSettingsWithApiVersionHeader =
298300
httpJsonComplianceClient
299301
.getSettings()

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITAutoPopulatedFields.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.google.showcase.v1beta1.it;
1717

18-
import static org.junit.Assert.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1919

2020
import com.google.api.gax.httpjson.ApiMethodDescriptor;
2121
import com.google.api.gax.httpjson.ForwardingHttpJsonClientCall;
@@ -47,12 +47,12 @@
4747
import java.util.concurrent.ExecutionException;
4848
import java.util.concurrent.TimeUnit;
4949
import java.util.function.Consumer;
50-
import org.junit.After;
51-
import org.junit.Before;
52-
import org.junit.Test;
50+
import org.junit.jupiter.api.AfterEach;
51+
import org.junit.jupiter.api.BeforeEach;
52+
import org.junit.jupiter.api.Test;
5353
import org.threeten.bp.Duration;
5454

55-
public class ITAutoPopulatedFields {
55+
class ITAutoPopulatedFields {
5656

5757
private static class HttpJsonInterceptor implements HttpJsonClientInterceptor {
5858
private Consumer<Object> onRequestIntercepted;
@@ -120,8 +120,8 @@ public void sendMessage(ReqT message) {
120120
private EchoClient httpJsonClient;
121121
private EchoClient httpJsonClientWithRetries;
122122

123-
@Before
124-
public void createClients() throws Exception {
123+
@BeforeEach
124+
void createClients() throws Exception {
125125
RetrySettings defaultRetrySettings =
126126
RetrySettings.newBuilder()
127127
.setInitialRpcTimeout(Duration.ofMillis(5000L))
@@ -156,15 +156,22 @@ public void createClients() throws Exception {
156156
defaultRetrySettings, retryableCodes, ImmutableList.of(httpJsonInterceptor));
157157
}
158158

159-
@After
160-
public void destroyClient() {
159+
@AfterEach
160+
void destroyClient() throws InterruptedException {
161161
grpcClientWithoutRetries.close();
162162
grpcClientWithRetries.close();
163163
httpJsonClient.close();
164+
165+
grpcClientWithoutRetries.awaitTermination(
166+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
167+
grpcClientWithRetries.awaitTermination(
168+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
169+
httpJsonClient.awaitTermination(
170+
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
164171
}
165172

166173
@Test
167-
public void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
174+
void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
168175
List<String> capturedRequestIds = new ArrayList<>();
169176
grpcRequestInterceptor.setOnRequestIntercepted(
170177
request -> {
@@ -181,7 +188,7 @@ public void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
181188
}
182189

183190
@Test
184-
public void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
191+
void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
185192
List<String> capturedRequestIds = new ArrayList<>();
186193
grpcRequestInterceptor.setOnRequestIntercepted(
187194
request -> {
@@ -197,7 +204,7 @@ public void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
197204
}
198205

199206
@Test
200-
public void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
207+
void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
201208
List<String> capturedRequestIds = new ArrayList<>();
202209
httpJsonInterceptor.setOnRequestIntercepted(
203210
request -> {
@@ -214,7 +221,7 @@ public void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
214221
}
215222

216223
@Test
217-
public void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
224+
void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
218225
String UUIDsent = UUID.randomUUID().toString();
219226
List<String> capturedRequestIds = new ArrayList<>();
220227
httpJsonInterceptor.setOnRequestIntercepted(
@@ -230,7 +237,7 @@ public void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
230237
}
231238

232239
@Test
233-
public void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
240+
void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
234241
List<String> capturedRequestIds = new ArrayList<>();
235242
grpcRequestInterceptor.setOnRequestIntercepted(
236243
request -> {
@@ -264,7 +271,7 @@ public void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() thr
264271
}
265272

266273
@Test
267-
public void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
274+
void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
268275
List<String> capturedRequestIds = new ArrayList<>();
269276
grpcRequestInterceptor.setOnRequestIntercepted(
270277
request -> {
@@ -302,8 +309,7 @@ public void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() thro
302309
}
303310

304311
@Test
305-
public void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried()
306-
throws Exception {
312+
void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
307313
List<String> capturedRequestIds = new ArrayList<>();
308314
httpJsonInterceptor.setOnRequestIntercepted(
309315
request -> {
@@ -336,7 +342,7 @@ public void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried()
336342
}
337343

338344
@Test
339-
public void testHttpJson_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
345+
void testHttpJson_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
340346
List<String> capturedRequestIds = new ArrayList<>();
341347
httpJsonInterceptor.setOnRequestIntercepted(
342348
request -> {

showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITBidiStreaming.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@
3030
import java.util.Arrays;
3131
import java.util.List;
3232
import java.util.concurrent.TimeUnit;
33-
import org.junit.AfterClass;
34-
import org.junit.BeforeClass;
35-
import org.junit.Test;
33+
import org.junit.jupiter.api.AfterAll;
34+
import org.junit.jupiter.api.BeforeAll;
35+
import org.junit.jupiter.api.Test;
3636

37-
public class ITBidiStreaming {
37+
class ITBidiStreaming {
3838

3939
private static EchoClient grpcClient;
4040

41-
@BeforeClass
42-
public static void createClients() throws Exception {
41+
@BeforeAll
42+
static void createClients() throws Exception {
4343
// Create gRPC Echo Client
4444
grpcClient = TestClientInitializer.createGrpcEchoClient();
4545
}
4646

47-
@AfterClass
48-
public static void destroyClients() throws Exception {
47+
@AfterAll
48+
static void destroyClients() throws Exception {
4949
grpcClient.close();
5050
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
5151
}
@@ -57,7 +57,7 @@ public static void destroyClients() throws Exception {
5757
// three requests, respond twice for every request etc. If that happens, the response content may
5858
// not be exactly the same as request content.
5959
@Test
60-
public void testGrpc_splitCall_shouldListensToResponse() throws Exception {
60+
void testGrpc_splitCall_shouldListensToResponse() throws Exception {
6161
// given
6262
List<String> expected = Arrays.asList("The rain in Spain stays mainly on the plain".split(" "));
6363
TestResponseObserver responseObserver = new TestResponseObserver();

0 commit comments

Comments
 (0)