Skip to content

Commit 38ebcc3

Browse files
authored
test: Migrate gax unit tests to Junit 5 (#2724)
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.
1 parent 94ef4f4 commit 38ebcc3

File tree

143 files changed

+2143
-2563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+2143
-2563
lines changed

gax-java/dependencies.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ maven.org_graalvm_sdk=org.graalvm.sdk:graal-sdk:22.3.5
7979

8080
# Testing maven artifacts
8181
maven.junit_junit=junit:junit:4.13.2
82-
maven.org_mockito_mockito_core=org.mockito:mockito-core:2.28.2
82+
maven.org_mockito_mockito_core=org.mockito:mockito-core:4.11.0
83+
maven.org_mockito_mockito_junit_jupiter=org.mockito:mockito-junit-jupiter:4.11.0
8384
maven.org_hamcrest_hamcrest_core=org.hamcrest:hamcrest-core:1.3
8485
maven.com_google_truth_truth=com.google.truth:truth:1.4.2
8586
maven.com_googlecode_java_diff_utils_diffutils=com.googlecode.java-diff-utils:diffutils:1.3.0
8687
maven.net_bytebuddy_byte_buddy=net.bytebuddy:byte-buddy:1.14.15
8788
maven.org_objenesis_objenesis=org.objenesis:objenesis:2.6
89+
maven.org_junit_jupiter_junit_jupiter_api=org.junit.jupiter:junit-jupiter-api:5.10.2
90+
maven.org_junit_jupiter_junit_jupiter_params=org.junit.jupiter:junit-jupiter-params:5.10.2

gax-java/gax-grpc/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ _COMPILE_DEPS = [
3535
]
3636

3737
_TEST_COMPILE_DEPS = [
38-
"@junit_junit//jar",
38+
"@org_junit_jupiter_junit_jupiter_api//jar",
3939
"@org_mockito_mockito_core//jar",
40+
"@org_mockito_mockito_junit_jupiter//jar",
4041
"@com_google_truth_truth//jar",
4142
"@io_grpc_grpc_java//core:inprocess",
4243
"@com_google_api_grpc_grpc_google_common_protos//jar",

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/CallOptionsUtilTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@
2929
*/
3030
package com.google.api.gax.grpc;
3131

32-
import static org.junit.Assert.assertEquals;
33-
import static org.junit.Assert.assertSame;
34-
import static org.junit.Assert.assertTrue;
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertSame;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3536

3637
import com.google.common.collect.ImmutableMap;
3738
import io.grpc.CallOptions;
3839
import io.grpc.Metadata.Key;
3940
import java.util.Map;
40-
import org.junit.Test;
41+
import org.junit.jupiter.api.Test;
4142

42-
public class CallOptionsUtilTest {
43+
class CallOptionsUtilTest {
4344
@Test
44-
public void testPutAndGetDynamicHeaderOption() {
45+
void testPutAndGetDynamicHeaderOption() {
4546
String encodedRequestParams = "param1=value&param2.param3=value23";
4647
CallOptions options =
4748
CallOptionsUtil.putRequestParamsDynamicHeaderOption(
@@ -54,16 +55,18 @@ public void testPutAndGetDynamicHeaderOption() {
5455
}
5556

5657
@Test
57-
public void testPutAndGetDynamicHeaderOptionEmpty() {
58+
void testPutAndGetDynamicHeaderOptionEmpty() {
5859
CallOptions options =
5960
CallOptionsUtil.putRequestParamsDynamicHeaderOption(CallOptions.DEFAULT, "");
6061
assertSame(CallOptions.DEFAULT, options);
6162
Map<Key<String>, String> headers = CallOptionsUtil.getDynamicHeadersOption(options);
6263
assertTrue(headers.isEmpty());
6364
}
6465

65-
@Test(expected = NullPointerException.class)
66-
public void testPutAndGetHeaderOptionNull() {
67-
CallOptionsUtil.putRequestParamsDynamicHeaderOption(CallOptions.DEFAULT, null);
66+
@Test
67+
void testPutAndGetHeaderOptionNull() {
68+
assertThrows(
69+
NullPointerException.class,
70+
() -> CallOptionsUtil.putRequestParamsDynamicHeaderOption(CallOptions.DEFAULT, null));
6871
}
6972
}

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/ChannelPoolTest.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,26 @@
6969
import java.util.concurrent.ScheduledFuture;
7070
import java.util.concurrent.TimeUnit;
7171
import java.util.concurrent.atomic.AtomicInteger;
72-
import org.junit.After;
73-
import org.junit.Assert;
74-
import org.junit.Test;
75-
import org.junit.runner.RunWith;
76-
import org.junit.runners.JUnit4;
72+
import org.junit.jupiter.api.AfterEach;
73+
import org.junit.jupiter.api.Assertions;
74+
import org.junit.jupiter.api.Test;
7775
import org.mockito.ArgumentCaptor;
7876
import org.mockito.Mockito;
7977
import org.mockito.stubbing.Answer;
8078

81-
@RunWith(JUnit4.class)
82-
public class ChannelPoolTest {
79+
class ChannelPoolTest {
8380
private static final int DEFAULT_AWAIT_TERMINATION_SEC = 10;
8481
private ChannelPool pool;
8582

86-
@After
87-
public void cleanup() throws InterruptedException {
83+
@AfterEach
84+
void cleanup() throws InterruptedException {
8885
Preconditions.checkNotNull(pool, "Channel pool was never created");
8986
pool.shutdown();
9087
pool.awaitTermination(DEFAULT_AWAIT_TERMINATION_SEC, TimeUnit.SECONDS);
9188
}
9289

9390
@Test
94-
public void testAuthority() throws IOException {
91+
void testAuthority() throws IOException {
9592
ManagedChannel sub1 = Mockito.mock(ManagedChannel.class);
9693
ManagedChannel sub2 = Mockito.mock(ManagedChannel.class);
9794

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

107104
@Test
108-
public void testRoundRobin() throws IOException {
105+
void testRoundRobin() throws IOException {
109106
ManagedChannel sub1 = Mockito.mock(ManagedChannel.class);
110107
ManagedChannel sub2 = Mockito.mock(ManagedChannel.class);
111108

@@ -144,7 +141,7 @@ private void verifyTargetChannel(
144141
}
145142

146143
@Test
147-
public void ensureEvenDistribution() throws InterruptedException, IOException {
144+
void ensureEvenDistribution() throws InterruptedException, IOException {
148145
int numChannels = 10;
149146
final ManagedChannel[] channels = new ManagedChannel[numChannels];
150147
final AtomicInteger[] counts = new AtomicInteger[numChannels];
@@ -197,7 +194,7 @@ public void ensureEvenDistribution() throws InterruptedException, IOException {
197194

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

216213
// Test channelPrimer is called periodically, if there's an executorService
217214
@Test
218-
public void channelPrimerIsCalledPeriodically() throws IOException {
215+
void channelPrimerIsCalledPeriodically() throws IOException {
219216
ChannelPrimer mockChannelPrimer = Mockito.mock(ChannelPrimer.class);
220217
ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
221218
ManagedChannel channel2 = Mockito.mock(ManagedChannel.class);
@@ -266,7 +263,7 @@ public void channelPrimerIsCalledPeriodically() throws IOException {
266263
// ----
267264
// call should be allowed to complete and the channel should not be shutdown
268265
@Test
269-
public void callShouldCompleteAfterCreation() throws IOException {
266+
void callShouldCompleteAfterCreation() throws IOException {
270267
ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
271268
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);
272269
FakeChannelFactory channelFactory =
@@ -314,7 +311,7 @@ public void callShouldCompleteAfterCreation() throws IOException {
314311

315312
// call should be allowed to complete and the channel should not be shutdown
316313
@Test
317-
public void callShouldCompleteAfterStarted() throws IOException {
314+
void callShouldCompleteAfterStarted() throws IOException {
318315
final ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
319316
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);
320317

@@ -359,7 +356,7 @@ public void callShouldCompleteAfterStarted() throws IOException {
359356

360357
// Channel should be shutdown after a refresh all the calls have completed
361358
@Test
362-
public void channelShouldShutdown() throws IOException {
359+
void channelShouldShutdown() throws IOException {
363360
ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
364361
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);
365362

@@ -402,7 +399,7 @@ public void channelShouldShutdown() throws IOException {
402399
}
403400

404401
@Test
405-
public void channelRefreshShouldSwapChannels() throws IOException {
402+
void channelRefreshShouldSwapChannels() throws IOException {
406403
ManagedChannel underlyingChannel1 = Mockito.mock(ManagedChannel.class);
407404
ManagedChannel underlyingChannel2 = Mockito.mock(ManagedChannel.class);
408405

@@ -442,7 +439,7 @@ public void channelRefreshShouldSwapChannels() throws IOException {
442439
}
443440

444441
@Test
445-
public void channelCountShouldNotChangeWhenOutstandingRpcsAreWithinLimits() throws Exception {
442+
void channelCountShouldNotChangeWhenOutstandingRpcsAreWithinLimits() throws Exception {
446443
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
447444

448445
List<ManagedChannel> channels = new ArrayList<>();
@@ -521,7 +518,7 @@ public void channelCountShouldNotChangeWhenOutstandingRpcsAreWithinLimits() thro
521518
}
522519

523520
@Test
524-
public void removedIdleChannelsAreShutdown() throws Exception {
521+
void removedIdleChannelsAreShutdown() throws Exception {
525522
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
526523

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

563560
@Test
564-
public void removedActiveChannelsAreShutdown() throws Exception {
561+
void removedActiveChannelsAreShutdown() throws Exception {
565562
ScheduledExecutorService executor = Mockito.mock(ScheduledExecutorService.class);
566563

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

624621
@Test
625-
public void testReleasingClientCallCancelEarly() throws IOException {
622+
void testReleasingClientCallCancelEarly() throws IOException {
626623
ClientCall mockClientCall = Mockito.mock(ClientCall.class);
627624
Mockito.doAnswer(invocation -> null).when(mockClientCall).cancel(Mockito.any(), Mockito.any());
628625
ManagedChannel fakeChannel = Mockito.mock(ManagedChannel.class);
@@ -650,7 +647,7 @@ public void testReleasingClientCallCancelEarly() throws IOException {
650647
Color request = Color.newBuilder().setRed(0.5f).build();
651648

652649
IllegalStateException e =
653-
Assert.assertThrows(
650+
Assertions.assertThrows(
654651
IllegalStateException.class,
655652
() ->
656653
streamingCallable.call(
@@ -675,7 +672,7 @@ public void onComplete() {}
675672
}
676673

677674
@Test
678-
public void testDoubleRelease() throws Exception {
675+
void testDoubleRelease() throws Exception {
679676
FakeLogHandler logHandler = new FakeLogHandler();
680677
ChannelPool.LOG.addHandler(logHandler);
681678

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GaxGrpcPropertiesTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,28 @@
2929
*/
3030
package com.google.api.gax.grpc;
3131

32-
import static org.junit.Assert.assertNotNull;
33-
import static org.junit.Assert.assertTrue;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3434

3535
import java.util.regex.Pattern;
36-
import org.junit.Test;
37-
import org.junit.runner.RunWith;
38-
import org.junit.runners.JUnit4;
36+
import org.junit.jupiter.api.Test;
3937

40-
@RunWith(JUnit4.class)
41-
public class GaxGrpcPropertiesTest {
38+
class GaxGrpcPropertiesTest {
4239

4340
@Test
44-
public void testGrpcVersion() {
41+
void testGrpcVersion() {
4542
String grpcVersion = GaxGrpcProperties.getGrpcVersion();
4643
assertTrue(Pattern.compile("^\\d+\\.\\d+\\.\\d+").matcher(grpcVersion).find());
4744
}
4845

4946
@Test
50-
public void testGaxGrpcVersion() {
47+
void testGaxGrpcVersion() {
5148
String gaxGrpcVersion = GaxGrpcProperties.getGaxGrpcVersion();
5249
assertNotNull(gaxGrpcVersion);
5350
}
5451

5552
@Test
56-
public void testDefaultHeaderPattern() {
53+
void testDefaultHeaderPattern() {
5754
assertTrue(
5855
GaxGrpcProperties.getDefaultApiClientHeaderPattern()
5956
.matcher("gl-java/1.8_00 gapic/1.2.3-alpha gax/1.5.0 grpc/1.7.0")

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcApiExceptionFactoryTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@
4444
import io.grpc.StatusException;
4545
import io.grpc.StatusRuntimeException;
4646
import java.util.Collections;
47-
import org.junit.Before;
48-
import org.junit.Test;
49-
import org.junit.runner.RunWith;
50-
import org.junit.runners.JUnit4;
47+
import org.junit.jupiter.api.BeforeEach;
48+
import org.junit.jupiter.api.Test;
5149

52-
@RunWith(JUnit4.class)
53-
public class GrpcApiExceptionFactoryTest {
50+
class GrpcApiExceptionFactoryTest {
5451

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

7370
private GrpcApiExceptionFactory factory;
7471

75-
@Before
76-
public void setUp() throws Exception {
72+
@BeforeEach
73+
void setUp() throws Exception {
7774
factory = new GrpcApiExceptionFactory(Collections.emptySet());
7875
}
7976

8077
@Test
81-
public void create_shouldCreateApiExceptionWithErrorDetailsForStatusException() {
78+
void create_shouldCreateApiExceptionWithErrorDetailsForStatusException() {
8279
Metadata trailers = new Metadata();
8380
Status status = Status.newBuilder().addAllDetails(RAW_ERROR_MESSAGES).build();
8481
trailers.put(
@@ -91,7 +88,7 @@ public void create_shouldCreateApiExceptionWithErrorDetailsForStatusException()
9188
}
9289

9390
@Test
94-
public void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeException() {
91+
void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeException() {
9592
Metadata trailers = new Metadata();
9693
Status status = Status.newBuilder().addAllDetails(RAW_ERROR_MESSAGES).build();
9794
trailers.put(
@@ -104,7 +101,7 @@ public void create_shouldCreateApiExceptionWithErrorDetailsForStatusRuntimeExcep
104101
}
105102

106103
@Test
107-
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataIsNull() {
104+
void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataIsNull() {
108105
StatusRuntimeException statusException = new StatusRuntimeException(GRPC_STATUS, null);
109106

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

115112
@Test
116-
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHaveErrorDetails() {
113+
void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHaveErrorDetails() {
117114
StatusRuntimeException statusException =
118115
new StatusRuntimeException(GRPC_STATUS, new Metadata());
119116

@@ -123,7 +120,7 @@ public void create_shouldCreateApiExceptionWithNoErrorDetailsIfMetadataDoesNotHa
123120
}
124121

125122
@Test
126-
public void create_shouldCreateApiExceptionWithNoErrorDetailsIfStatusIsMalformed() {
123+
void create_shouldCreateApiExceptionWithNoErrorDetailsIfStatusIsMalformed() {
127124
Metadata trailers = new Metadata();
128125
Status status = Status.newBuilder().addDetails(Any.pack(ERROR_INFO)).build();
129126
byte[] bytes = status.toByteArray();

0 commit comments

Comments
 (0)