Skip to content

Commit

Permalink
chore: Migrate Junit 4 to Junit 5 for showcase (#2757)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people committed May 22, 2024
1 parent cda8a9d commit fd91c65
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 351 deletions.
20 changes: 18 additions & 2 deletions showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,30 @@

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.4.2</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.google.showcase.v1beta1.it;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.api.gax.httpjson.*;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
Expand All @@ -31,15 +31,15 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

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

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

private HttpJsonCapturingClientInterceptor httpJsonInterceptor;
private GrpcCapturingClientInterceptor grpcInterceptor;
private HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
private GrpcCapturingClientInterceptor grpcComplianceInterceptor;
private EchoClient grpcClient;
private EchoClient httpJsonClient;
private ComplianceClient grpcComplianceClient;
private ComplianceClient httpJsonComplianceClient;

@Before
public void createClients() throws Exception {
private static HttpJsonCapturingClientInterceptor httpJsonInterceptor;
private static GrpcCapturingClientInterceptor grpcInterceptor;
private static HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
private static GrpcCapturingClientInterceptor grpcComplianceInterceptor;
private static EchoClient grpcClient;
private static EchoClient httpJsonClient;
private static ComplianceClient grpcComplianceClient;
private static ComplianceClient httpJsonComplianceClient;

@BeforeAll
static void createClients() throws Exception {
// Create gRPC Interceptor and Client
grpcInterceptor = new GrpcCapturingClientInterceptor();
grpcClient = TestClientInitializer.createGrpcEchoClient(ImmutableList.of(grpcInterceptor));
Expand All @@ -183,28 +182,31 @@ public void createClients() throws Exception {
ImmutableList.of(httpJsonComplianceInterceptor));
}

@After
public void destroyClient() throws InterruptedException {
@AfterAll
static void destroyClient() throws InterruptedException {
grpcClient.close();
httpJsonClient.close();
grpcComplianceClient.close();
httpJsonComplianceClient.close();

grpcClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
httpJsonClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
grpcComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
httpJsonComplianceClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpJsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
grpcComplianceClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpJsonComplianceClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
public void testGrpc_matchesApiVersion() {
void testGrpc_matchesApiVersion() {
grpcClient.echo(EchoRequest.newBuilder().build());
String headerValue = grpcInterceptor.metadata.get(API_VERSION_HEADER_KEY);
assertThat(headerValue).isEqualTo(EXPECTED_ECHO_API_VERSION);
}

@Test
public void testHttpJson_matchesHeaderName() {
void testHttpJson_matchesHeaderName() {
httpJsonClient.echo(EchoRequest.newBuilder().build());
ArrayList headerValues =
(ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
Expand All @@ -213,15 +215,15 @@ public void testHttpJson_matchesHeaderName() {
}

@Test
public void testGrpc_noApiVersion() {
void testGrpc_noApiVersion() {
RepeatRequest request =
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
grpcComplianceClient.repeatDataSimplePath(request);
assertThat(API_VERSION_HEADER_KEY).isNotIn(grpcComplianceInterceptor.metadata.keys());
}

@Test
public void testHttpJson_noApiVersion() {
void testHttpJson_noApiVersion() {
RepeatRequest request =
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
httpJsonComplianceClient.repeatDataSimplePath(request);
Expand All @@ -230,7 +232,7 @@ public void testHttpJson_noApiVersion() {
}

@Test
public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
void testGrpcEcho_userApiVersionThrowsException() throws IOException {
StubSettings stubSettings =
grpcClient
.getSettings()
Expand All @@ -249,7 +251,7 @@ public void testGrpcEcho_userApiVersionThrowsException() throws IOException {
}

@Test
public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
void testHttpJsonEcho_userApiVersionThrowsException() throws IOException {
StubSettings stubSettings =
httpJsonClient
.getSettings()
Expand All @@ -268,7 +270,7 @@ public void testHttpJsonEcho_userApiVersionThrowsException() throws IOException
}

@Test
public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
StubSettings stubSettingsWithApiVersionHeader =
grpcComplianceClient
.getSettings()
Expand All @@ -293,7 +295,7 @@ public void testGrpcCompliance_userApiVersionSetSuccess() throws IOException {
}

@Test
public void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {
void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {
StubSettings httpJsonStubSettingsWithApiVersionHeader =
httpJsonComplianceClient
.getSettings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.showcase.v1beta1.it;

import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.api.gax.httpjson.ApiMethodDescriptor;
import com.google.api.gax.httpjson.ForwardingHttpJsonClientCall;
Expand Down Expand Up @@ -47,12 +47,12 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.threeten.bp.Duration;

public class ITAutoPopulatedFields {
class ITAutoPopulatedFields {

private static class HttpJsonInterceptor implements HttpJsonClientInterceptor {
private Consumer<Object> onRequestIntercepted;
Expand Down Expand Up @@ -120,8 +120,8 @@ public void sendMessage(ReqT message) {
private EchoClient httpJsonClient;
private EchoClient httpJsonClientWithRetries;

@Before
public void createClients() throws Exception {
@BeforeEach
void createClients() throws Exception {
RetrySettings defaultRetrySettings =
RetrySettings.newBuilder()
.setInitialRpcTimeout(Duration.ofMillis(5000L))
Expand Down Expand Up @@ -156,15 +156,22 @@ public void createClients() throws Exception {
defaultRetrySettings, retryableCodes, ImmutableList.of(httpJsonInterceptor));
}

@After
public void destroyClient() {
@AfterEach
void destroyClient() throws InterruptedException {
grpcClientWithoutRetries.close();
grpcClientWithRetries.close();
httpJsonClient.close();

grpcClientWithoutRetries.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
grpcClientWithRetries.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
httpJsonClient.awaitTermination(
TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
}

@Test
public void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand All @@ -181,7 +188,7 @@ public void testGrpc_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
}

@Test
public void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand All @@ -197,7 +204,7 @@ public void testGrpc_shouldNotAutoPopulateRequestIdIfSetInRequest() {
}

@Test
public void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
request -> {
Expand All @@ -214,7 +221,7 @@ public void testHttpJson_autoPopulateRequestIdWhenAttemptedOnceSuccessfully() {
}

@Test
public void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
String UUIDsent = UUID.randomUUID().toString();
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
Expand All @@ -230,7 +237,7 @@ public void testHttpJson_shouldNotAutoPopulateRequestIdIfSetInRequest() {
}

@Test
public void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand Down Expand Up @@ -264,7 +271,7 @@ public void testGRPC_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() thr
}

@Test
public void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
grpcRequestInterceptor.setOnRequestIntercepted(
request -> {
Expand Down Expand Up @@ -302,8 +309,7 @@ public void testGRPC_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() thro
}

@Test
public void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried()
throws Exception {
void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
request -> {
Expand Down Expand Up @@ -336,7 +342,7 @@ public void testHttpJson_setsSameRequestIdIfSetInRequestWhenRequestsAreRetried()
}

@Test
public void testHttpJson_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
void testHttpJson_setsSameAutoPopulatedRequestIdWhenRequestsAreRetried() throws Exception {
List<String> capturedRequestIds = new ArrayList<>();
httpJsonInterceptor.setOnRequestIntercepted(
request -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ITBidiStreaming {
class ITBidiStreaming {

private static EchoClient grpcClient;

@BeforeClass
public static void createClients() throws Exception {
@BeforeAll
static void createClients() throws Exception {
// Create gRPC Echo Client
grpcClient = TestClientInitializer.createGrpcEchoClient();
}

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

0 comments on commit fd91c65

Please sign in to comment.