Skip to content

Commit

Permalink
chore: use Junit5 in java core (#2754)
Browse files Browse the repository at this point in the history
Fix #2726.

`BaseSerializationTest` will not migrate to Junit 5 because downstream
libraries, e.g., java-logging, are extending this class and these
libraries still use Junit 4. Migrating this class to Junit 5 will cause
test failures in downstream libraries.
  • Loading branch information
JoeWang1127 authored and lqiu96 committed May 22, 2024
1 parent 986c36e commit 37d7e41
Show file tree
Hide file tree
Showing 38 changed files with 521 additions and 536 deletions.
1 change: 1 addition & 0 deletions gapic-generator-java-pom-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<errorprone.version>2.27.1</errorprone.version>
<j2objc-annotations.version>3.0.0</j2objc-annotations.version>
<threetenbp.version>1.6.9</threetenbp.version>
<junit.version>5.10.2</junit.version>
</properties>

<developers>
Expand Down
14 changes: 12 additions & 2 deletions java-core/google-cloud-core-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.InternalException;
Expand All @@ -33,16 +33,16 @@
import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class BaseGrpcServiceExceptionTest {
class BaseGrpcServiceExceptionTest {

private static final String MESSAGE = "some message";
private static final boolean NOT_RETRYABLE = false;
private static final boolean IDEMPOTENT = true;

@Test
public void testBaseServiceException() {
void testBaseServiceException() {
BaseGrpcServiceException serviceException = null;

IOException exception = new SocketTimeoutException();
Expand Down Expand Up @@ -86,7 +86,7 @@ public void testBaseServiceException() {
}

@Test
public void testTranslateAndThrow() throws Exception {
void testTranslateAndThrow() throws Exception {
IOException exception = new SocketTimeoutException();
BaseGrpcServiceException cause = new BaseGrpcServiceException(exception, IDEMPOTENT);
RetryHelper.RetryHelperException exceptionMock =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package com.google.cloud.grpc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.cloud.grpc.GrpcTransportOptions.DefaultExecutorFactory;
import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory;
import java.util.concurrent.ScheduledExecutorService;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class GrpcTransportOptionsTest {
class GrpcTransportOptionsTest {

private static final ExecutorFactory MOCK_EXECUTOR_FACTORY =
EasyMock.createMock(ExecutorFactory.class);
Expand All @@ -38,13 +38,13 @@ public class GrpcTransportOptionsTest {
private static final GrpcTransportOptions OPTIONS_COPY = OPTIONS.toBuilder().build();

@Test
public void testBuilder() {
void testBuilder() {
assertSame(MOCK_EXECUTOR_FACTORY, OPTIONS.getExecutorFactory());
assertTrue(DEFAULT_OPTIONS.getExecutorFactory() instanceof DefaultExecutorFactory);
}

@Test
public void testBaseEquals() {
void testBaseEquals() {
assertEquals(OPTIONS, OPTIONS_COPY);
assertNotEquals(DEFAULT_OPTIONS, OPTIONS);
GrpcTransportOptions options =
Expand All @@ -53,7 +53,7 @@ public void testBaseEquals() {
}

@Test
public void testBaseHashCode() {
void testBaseHashCode() {
assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode());
assertNotEquals(DEFAULT_OPTIONS.hashCode(), OPTIONS.hashCode());
GrpcTransportOptions options =
Expand All @@ -62,7 +62,7 @@ public void testBaseHashCode() {
}

@Test
public void testDefaultExecutorFactory() {
void testDefaultExecutorFactory() {
ExecutorFactory<ScheduledExecutorService> executorFactory = new DefaultExecutorFactory();
ScheduledExecutorService executorService = executorFactory.get();
assertSame(executorService, executorFactory.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@

package com.google.cloud.grpc;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.LinkedList;
import java.util.concurrent.Delayed;
Expand All @@ -26,20 +37,17 @@
import java.util.concurrent.TimeUnit;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
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;

/**
* This class was copied from grpc-core to prevent dependence on an unstable API that may be subject
* to changes
* (https://github.com/grpc/grpc-java/blob/d07ecbe037d2705a1c9f4b6345581f860e505b56/core/src/test/java/io/grpc/internal/SharedResourceHolderTest.java)
* to changes (<a
* href="https://github.com/grpc/grpc-java/blob/d07ecbe037d2705a1c9f4b6345581f860e505b56/core/src/test/java/io/grpc/internal/SharedResourceHolderTest.java">SharedResourceHolderTest</a>)
*
* <p>Unit tests for {@link SharedResourceHolder}.
*/
@RunWith(JUnit4.class)
public class SharedResourceHolderTest {
class SharedResourceHolderTest {

private final LinkedList<MockScheduledFuture<?>> scheduledDestroyTasks = new LinkedList<>();

Expand Down Expand Up @@ -67,13 +75,13 @@ public void close(ResourceInstance instance) {
private static final SharedResourceHolder.Resource<ResourceInstance> SHARED_BAR =
new ResourceFactory();

@Before
public void setUp() {
@BeforeEach
void setUp() {
holder = new SharedResourceHolder(new MockExecutorFactory());
}

@Test
public void destroyResourceWhenRefCountReachesZero() {
void destroyResourceWhenRefCountReachesZero() {
ResourceInstance foo1 = holder.getInternal(SHARED_FOO);
ResourceInstance sharedFoo = foo1;
ResourceInstance foo2 = holder.getInternal(SHARED_FOO);
Expand Down Expand Up @@ -121,7 +129,7 @@ public void destroyResourceWhenRefCountReachesZero() {
}

@Test
public void cancelDestroyTask() {
void cancelDestroyTask() {
ResourceInstance foo1 = holder.getInternal(SHARED_FOO);
ResourceInstance sharedFoo = foo1;
holder.releaseInternal(SHARED_FOO, foo1);
Expand All @@ -148,7 +156,7 @@ public void cancelDestroyTask() {
}

@Test
public void releaseWrongInstance() {
void releaseWrongInstance() {
ResourceInstance uncached = new ResourceInstance();
try {
holder.releaseInternal(SHARED_FOO, uncached);
Expand All @@ -167,7 +175,7 @@ public void releaseWrongInstance() {
}

@Test
public void overreleaseInstance() {
void overreleaseInstance() {
ResourceInstance foo1 = holder.getInternal(SHARED_FOO);
holder.releaseInternal(SHARED_FOO, foo1);
try {
Expand All @@ -179,7 +187,7 @@ public void overreleaseInstance() {
}

@Test
public void handleInstanceCloseError() {
void handleInstanceCloseError() {
class ExceptionOnCloseResource implements SharedResourceHolder.Resource<ResourceInstance> {
@Override
public ResourceInstance create() {
Expand Down
14 changes: 12 additions & 2 deletions java-core/google-cloud-core-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.cloud.BaseServiceException;
Expand All @@ -35,9 +35,9 @@
import java.net.SocketTimeoutException;
import java.util.Collections;
import java.util.Set;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class BaseHttpServiceExceptionTest {
class BaseHttpServiceExceptionTest {

private static final int CODE = 1;
private static final int CODE_NO_REASON = 2;
Expand All @@ -53,7 +53,7 @@ private static class CustomServiceException extends BaseHttpServiceException {

private static final long serialVersionUID = -195251309124875103L;

public CustomServiceException(int code, String message, String reason, boolean idempotent) {
CustomServiceException(int code, String message, String reason, boolean idempotent) {
super(code, message, reason, idempotent, RETRYABLE_ERRORS);
}

Expand All @@ -63,7 +63,7 @@ public CustomServiceException(int code, String message, String reason, boolean i
}

@Test
public void testBaseServiceException() {
void testBaseServiceException() {
BaseServiceException serviceException =
new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
assertEquals(CODE, serviceException.getCode());
Expand Down Expand Up @@ -145,7 +145,7 @@ public void testBaseServiceException() {
}

@Test
public void testTranslateAndThrow() throws Exception {
void testTranslateAndThrow() throws Exception {
BaseServiceException cause =
new BaseHttpServiceException(CODE, MESSAGE, REASON, IDEMPOTENT, EMPTY_RETRYABLE_ERRORS);
RetryHelper.RetryHelperException exceptionMock =
Expand Down
Loading

0 comments on commit 37d7e41

Please sign in to comment.