Skip to content

Commit

Permalink
test: Migrate gax unit tests to Junit 5 (#2724)
Browse files Browse the repository at this point in the history
This PR migrate all unit tests in Gax to Junit 5.

Other than standard direct replacements, some tests need to be rewritten
due to the following scenarios:
- `@Rule` does not exist anymore and there is no direct replacement. We
mostly use it to initialize a Mockito stub, replaced with
`@ExtendWith(MockitoExtension.class)`.
- Some tests were relying on try-catch to assert exceptions, replaced
with `assertThrows`. e.g.
[OperationsClientTest](https://github.com/googleapis/sdk-platform-java/pull/2724/files#diff-4530df761eff0854357165d951e1667d3810a5448ec2aa4b853a6331516cbde0)
- Parameterized tests in
[AbstractRetryingExecutorTest](https://github.com/googleapis/sdk-platform-java/pull/2724/files#diff-9c5f5c1d2fcef6c4164fc0171d01e7020aa7ebb7aa49615cf3743dc89c9b3d1d)

There are a few environment variable tests can be re-written with Junit
5, so we don't need to configure a
[profile](https://github.com/googleapis/sdk-platform-java/blob/main/gax-java/gax/pom.xml#L115-L128)
for it anymore, but they are not in the scope of this PR.

fixes: #1611.
  • Loading branch information
blakeli0 committed May 17, 2024
1 parent 94ef4f4 commit 38ebcc3
Show file tree
Hide file tree
Showing 143 changed files with 2,143 additions and 2,563 deletions.
5 changes: 4 additions & 1 deletion gax-java/dependencies.properties
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ maven.org_graalvm_sdk=org.graalvm.sdk:graal-sdk:22.3.5

# Testing maven artifacts
maven.junit_junit=junit:junit:4.13.2
maven.org_mockito_mockito_core=org.mockito:mockito-core:2.28.2
maven.org_mockito_mockito_core=org.mockito:mockito-core:4.11.0
maven.org_mockito_mockito_junit_jupiter=org.mockito:mockito-junit-jupiter:4.11.0
maven.org_hamcrest_hamcrest_core=org.hamcrest:hamcrest-core:1.3
maven.com_google_truth_truth=com.google.truth:truth:1.4.2
maven.com_googlecode_java_diff_utils_diffutils=com.googlecode.java-diff-utils:diffutils:1.3.0
maven.net_bytebuddy_byte_buddy=net.bytebuddy:byte-buddy:1.14.15
maven.org_objenesis_objenesis=org.objenesis:objenesis:2.6
maven.org_junit_jupiter_junit_jupiter_api=org.junit.jupiter:junit-jupiter-api:5.10.2
maven.org_junit_jupiter_junit_jupiter_params=org.junit.jupiter:junit-jupiter-params:5.10.2
3 changes: 2 additions & 1 deletion gax-java/gax-grpc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ _COMPILE_DEPS = [
]

_TEST_COMPILE_DEPS = [
"@junit_junit//jar",
"@org_junit_jupiter_junit_jupiter_api//jar",
"@org_mockito_mockito_core//jar",
"@org_mockito_mockito_junit_jupiter//jar",
"@com_google_truth_truth//jar",
"@io_grpc_grpc_java//core:inprocess",
"@com_google_api_grpc_grpc_google_common_protos//jar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@
*/
package com.google.api.gax.grpc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.common.collect.ImmutableMap;
import io.grpc.CallOptions;
import io.grpc.Metadata.Key;
import java.util.Map;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class CallOptionsUtilTest {
class CallOptionsUtilTest {
@Test
public void testPutAndGetDynamicHeaderOption() {
void testPutAndGetDynamicHeaderOption() {
String encodedRequestParams = "param1=value&param2.param3=value23";
CallOptions options =
CallOptionsUtil.putRequestParamsDynamicHeaderOption(
Expand All @@ -54,16 +55,18 @@ public void testPutAndGetDynamicHeaderOption() {
}

@Test
public void testPutAndGetDynamicHeaderOptionEmpty() {
void testPutAndGetDynamicHeaderOptionEmpty() {
CallOptions options =
CallOptionsUtil.putRequestParamsDynamicHeaderOption(CallOptions.DEFAULT, "");
assertSame(CallOptions.DEFAULT, options);
Map<Key<String>, String> headers = CallOptionsUtil.getDynamicHeadersOption(options);
assertTrue(headers.isEmpty());
}

@Test(expected = NullPointerException.class)
public void testPutAndGetHeaderOptionNull() {
CallOptionsUtil.putRequestParamsDynamicHeaderOption(CallOptions.DEFAULT, null);
@Test
void testPutAndGetHeaderOptionNull() {
assertThrows(
NullPointerException.class,
() -> CallOptionsUtil.putRequestParamsDynamicHeaderOption(CallOptions.DEFAULT, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,26 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;

@RunWith(JUnit4.class)
public class ChannelPoolTest {
class ChannelPoolTest {
private static final int DEFAULT_AWAIT_TERMINATION_SEC = 10;
private ChannelPool pool;

@After
public void cleanup() throws InterruptedException {
@AfterEach
void cleanup() throws InterruptedException {
Preconditions.checkNotNull(pool, "Channel pool was never created");
pool.shutdown();
pool.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
}

@Test
public void testAuthority() throws IOException {
void testAuthority() throws IOException {
ManagedChannel sub1 = Mockito.mock(ManagedChannel.class);
ManagedChannel sub2 = Mockito.mock(ManagedChannel.class);

Expand All @@ -105,7 +102,7 @@ public void testAuthority() throws IOException {
}

@Test
public void testRoundRobin() throws IOException {
void testRoundRobin() throws IOException {
ManagedChannel sub1 = Mockito.mock(ManagedChannel.class);
ManagedChannel sub2 = Mockito.mock(ManagedChannel.class);

Expand Down Expand Up @@ -144,7 +141,7 @@ private void verifyTargetChannel(
}

@Test
public void ensureEvenDistribution() throws InterruptedException, IOException {
void ensureEvenDistribution() throws InterruptedException, IOException {
int numChannels = 10;
final ManagedChannel[] channels = new ManagedChannel[numChannels];
final AtomicInteger[] counts = new AtomicInteger[numChannels];
Expand Down Expand Up @@ -197,7 +194,7 @@ public void ensureEvenDistribution() throws InterruptedException, IOException {

// Test channelPrimer is called same number of times as poolSize if executorService is set to null
@Test
public void channelPrimerShouldCallPoolConstruction() throws IOException {
void channelPrimerShouldCallPoolConstruction() throws IOException {
ChannelPrimer mockChannelPrimer = Mockito.mock(ChannelPrimer.class);
ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
ManagedChannel channel2 = Mockito.mock(ManagedChannel.class);
Expand All @@ -215,7 +212,7 @@ public void channelPrimerShouldCallPoolConstruction() throws IOException {

// Test channelPrimer is called periodically, if there's an executorService
@Test
public void channelPrimerIsCalledPeriodically() throws IOException {
void channelPrimerIsCalledPeriodically() throws IOException {
ChannelPrimer mockChannelPrimer = Mockito.mock(ChannelPrimer.class);
ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
ManagedChannel channel2 = Mockito.mock(ManagedChannel.class);
Expand Down Expand Up @@ -266,7 +263,7 @@ public void channelPrimerIsCalledPeriodically() throws IOException {
// ----
// call should be allowed to complete and the channel should not be shutdown
@Test
public void callShouldCompleteAfterCreation() throws IOException {
void callShouldCompleteAfterCreation() throws IOException {
ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);
FakeChannelFactory channelFactory =
Expand Down Expand Up @@ -314,7 +311,7 @@ public void callShouldCompleteAfterCreation() throws IOException {

// call should be allowed to complete and the channel should not be shutdown
@Test
public void callShouldCompleteAfterStarted() throws IOException {
void callShouldCompleteAfterStarted() throws IOException {
final ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);

Expand Down Expand Up @@ -359,7 +356,7 @@ public void callShouldCompleteAfterStarted() throws IOException {

// Channel should be shutdown after a refresh all the calls have completed
@Test
public void channelShouldShutdown() throws IOException {
void channelShouldShutdown() throws IOException {
ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);

Expand Down Expand Up @@ -402,7 +399,7 @@ public void channelShouldShutdown() throws IOException {
}

@Test
public void channelRefreshShouldSwapChannels() throws IOException {
void channelRefreshShouldSwapChannels() throws IOException {
ManagedChannel underlyingChannel1 = Mockito.mock(ManagedChannel.class);
ManagedChannel underlyingChannel2 = Mockito.mock(ManagedChannel.class);

Expand Down Expand Up @@ -442,7 +439,7 @@ public void channelRefreshShouldSwapChannels() throws IOException {
}

@Test
public void channelCountShouldNotChangeWhenOutstandingRpcsAreWithinLimits() throws Exception {
void channelCountShouldNotChangeWhenOutstandingRpcsAreWithinLimits() throws Exception {
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);

List<ManagedChannel> channels = new ArrayList<>();
Expand Down Expand Up @@ -521,7 +518,7 @@ public void channelCountShouldNotChangeWhenOutstandingRpcsAreWithinLimits() thro
}

@Test
public void removedIdleChannelsAreShutdown() throws Exception {
void removedIdleChannelsAreShutdown() throws Exception {
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);

List<ManagedChannel> channels = new ArrayList<>();
Expand Down Expand Up @@ -561,7 +558,7 @@ public void removedIdleChannelsAreShutdown() throws Exception {
}

@Test
public void removedActiveChannelsAreShutdown() throws Exception {
void removedActiveChannelsAreShutdown() throws Exception {
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);

List<ManagedChannel> channels = new ArrayList<>();
Expand Down Expand Up @@ -622,7 +619,7 @@ public void removedActiveChannelsAreShutdown() throws Exception {
}

@Test
public void testReleasingClientCallCancelEarly() throws IOException {
void testReleasingClientCallCancelEarly() throws IOException {
ClientCall mockClientCall = Mockito.mock(ClientCall.class);
Mockito.doAnswer(invocation -> null).when(mockClientCall).cancel(Mockito.any(), Mockito.any());
ManagedChannel fakeChannel = Mockito.mock(ManagedChannel.class);
Expand Down Expand Up @@ -650,7 +647,7 @@ public void testReleasingClientCallCancelEarly() throws IOException {
Color request = Color.newBuilder().setRed(0.5f).build();

IllegalStateException e =
Assert.assertThrows(
Assertions.assertThrows(
IllegalStateException.class,
() ->
streamingCallable.call(
Expand All @@ -675,7 +672,7 @@ public void onComplete() {}
}

@Test
public void testDoubleRelease() throws Exception {
void testDoubleRelease() throws Exception {
FakeLogHandler logHandler = new FakeLogHandler();
ChannelPool.LOG.addHandler(logHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,28 @@
*/
package com.google.api.gax.grpc;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

@RunWith(JUnit4.class)
public class GaxGrpcPropertiesTest {
class GaxGrpcPropertiesTest {

@Test
public void testGrpcVersion() {
void testGrpcVersion() {
String grpcVersion = GaxGrpcProperties.getGrpcVersion();
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(grpcVersion).find());
}

@Test
public void testGaxGrpcVersion() {
void testGaxGrpcVersion() {
String gaxGrpcVersion = GaxGrpcProperties.getGaxGrpcVersion();
assertNotNull(gaxGrpcVersion);
}

@Test
public void testDefaultHeaderPattern() {
void testDefaultHeaderPattern() {
assertTrue(
GaxGrpcProperties.getDefaultApiClientHeaderPattern()
.matcher("gl-java/1.8_00 gapic/1.2.3-alpha gax/1.5.0 grpc/1.7.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@RunWith(JUnit4.class)
public class GrpcApiExceptionFactoryTest {
class GrpcApiExceptionFactoryTest {

private static final ErrorInfo ERROR_INFO =
ErrorInfo.newBuilder()
Expand All @@ -72,13 +69,13 @@ public class GrpcApiExceptionFactoryTest {

private GrpcApiExceptionFactory factory;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
factory = new GrpcApiExceptionFactory(Collections.emptySet());
}

@Test
public void create_shouldCreateApiExceptionWithErrorDetailsForStatusException() {
void create_shouldCreateApiExceptionWithErrorDetailsForStatusException() {
Metadata trailers = new Metadata();
Status status = Status.newBuilder().addAllDetails(RAW_ERROR_MESSAGES).build();
trailers.put(
Expand All @@ -91,7 +88,7 @@ public void create_shouldCreateApiExceptionWithErrorDetailsForStatusException()
}

@Test
public void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeException() {
void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeException() {
Metadata trailers = new Metadata();
Status status = Status.newBuilder().addAllDetails(RAW_ERROR_MESSAGES).build();
trailers.put(
Expand All @@ -104,7 +101,7 @@ public void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeExcep
}

@Test
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataIsNull() {
void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataIsNull() {
StatusRuntimeException statusException = new StatusRuntimeException(GRPC_STATUS, null);

ApiException actual = factory.create(statusException);
Expand All @@ -113,7 +110,7 @@ public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataIsNull()
}

@Test
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHaveErrorDetails() {
void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHaveErrorDetails() {
StatusRuntimeException statusException =
new StatusRuntimeException(GRPC_STATUS, new Metadata());

Expand All @@ -123,7 +120,7 @@ public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHa
}

@Test
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfStatusIsMalformed() {
void create_shouldCreateApiExceptionWithNoErrorDetailsIfStatusIsMalformed() {
Metadata trailers = new Metadata();
Status status = Status.newBuilder().addDetails(Any.pack(ERROR_INFO)).build();
byte[] bytes = status.toByteArray();
Expand Down

0 comments on commit 38ebcc3

Please sign in to comment.