Skip to content

Commit

Permalink
Migrate servicetalk-grpc-* tests to junit5 (#1660)
Browse files Browse the repository at this point in the history
Motivation:

JUnit 5 leverages features from Java 8 or later, such as lambda functions, making tests more powerful and easier to maintain.
JUnit 5 has added some very useful new features for describing, organizing, and executing tests. For instance, tests get better display names and can be organized hierarchically.
JUnit 5 is organized into multiple libraries, so only the features you need are imported into your project. With build systems such as Maven and Gradle, including the right libraries is easy.
JUnit 5 can use more than one extension at a time, which JUnit 4 could not (only one runner could be used at a time). This means you can easily combine the Spring extension with other extensions (such as your own custom extension).

Modifications:

Unit tests have been migrated from JUnit 4 to JUnit 5

Result:

Modules servicetalk-grpc-* now run tests and test suits using JUnit 5
  • Loading branch information
danfaer authored and Dariusz Jędrzejczyk committed Jul 21, 2021
1 parent 0c91042 commit 8ac828c
Show file tree
Hide file tree
Showing 29 changed files with 923 additions and 772 deletions.
4 changes: 3 additions & 1 deletion servicetalk-grpc-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"

testImplementation project(":servicetalk-test-resources")
testImplementation "junit:junit:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
}
5 changes: 4 additions & 1 deletion servicetalk-grpc-netty/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ dependencies {
testImplementation "io.grpc:grpc-stub:$grpcVersion"
testImplementation "io.netty:netty-tcnative-boringssl-static:$tcnativeVersion"
testImplementation "jakarta.annotation:jakarta.annotation-api:$javaxAnnotationsApiVersion"
testImplementation "junit:junit:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5Version"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
}

protobuf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package io.servicetalk.grpc.api;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static io.servicetalk.http.netty.AsyncContextHttpFilterVerifier.verifyServerFilterAsyncContextVisibility;

public class CatchAllHttpServiceFilterTest {
class CatchAllHttpServiceFilterTest {

@Test
public void verifyAsyncContext() throws Exception {
void verifyAsyncContext() throws Exception {
verifyServerFilterAsyncContextVisibility(GrpcServerBuilder.CatchAllHttpServiceFilter::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.servicetalk.concurrent.GracefulAutoCloseable;
import io.servicetalk.concurrent.api.AsyncCloseable;
import io.servicetalk.concurrent.api.Completable;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;
import io.servicetalk.grpc.netty.TesterProto.Tester.BlockingTestBiDiStreamRpc;
import io.servicetalk.grpc.netty.TesterProto.Tester.BlockingTestRequestStreamRpc;
import io.servicetalk.grpc.netty.TesterProto.Tester.BlockingTestResponseStreamRpc;
Expand All @@ -31,13 +30,9 @@
import io.servicetalk.grpc.netty.TesterProto.Tester.TesterService;
import io.servicetalk.transport.api.ServerContext;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -46,7 +41,6 @@
import static io.servicetalk.grpc.netty.GrpcServers.forAddress;
import static io.servicetalk.grpc.netty.TesterProto.Tester.ServiceFactory;
import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand All @@ -57,42 +51,40 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@RunWith(Parameterized.class)
public class ClosureTest {
@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
class ClosureTest {

private final boolean closeGracefully;
private boolean closeGracefully;

public ClosureTest(final boolean closeGracefully) {
private void setUp(final boolean closeGracefully) {
this.closeGracefully = closeGracefully;
}

@Parameterized.Parameters(name = "graceful? => {0}")
public static Collection<Boolean> data() {
return asList(true, false);
}

@Test
public void serviceImplIsClosed() throws Exception {
@ParameterizedTest(name = "graceful? => {0}")
@ValueSource(booleans = {true, false})
void serviceImplIsClosed(final boolean param) throws Exception {
setUp(param);
CloseSignal signal = new CloseSignal(1);
TesterService svc = setupCloseMock(mock(TesterService.class), signal);
startServerAndClose(new ServiceFactory(svc), signal);
verifyClosure(svc, 4 /* 4 rpc methods */);
signal.verifyCloseAtLeastCount(closeGracefully);
}

@Test
public void blockingServiceImplIsClosed() throws Exception {
@ParameterizedTest(name = "graceful? => {0}")
@ValueSource(booleans = {true, false})
void blockingServiceImplIsClosed(final boolean param) throws Exception {
setUp(param);
CloseSignal signal = new CloseSignal(1);
BlockingTesterService svc = setupBlockingCloseMock(mock(BlockingTesterService.class), signal);
startServerAndClose(new ServiceFactory(svc), signal);
verifyClosure(svc, 4 /* 4 rpc methods */);
signal.verifyCloseAtLeastCount(closeGracefully);
}

@Test
public void rpcMethodsAreClosed() throws Exception {
@ParameterizedTest(name = "graceful? => {0}")
@ValueSource(booleans = {true, false})
void rpcMethodsAreClosed(final boolean param) throws Exception {
setUp(param);
CloseSignal signal = new CloseSignal(4);
TestRpc testRpc = setupCloseMock(mock(TestRpc.class), signal);
TestRequestStreamRpc testRequestStreamRpc = setupCloseMock(mock(TestRequestStreamRpc.class), signal);
Expand All @@ -111,8 +103,10 @@ public void rpcMethodsAreClosed() throws Exception {
signal.verifyClose(closeGracefully);
}

@Test
public void blockingRpcMethodsAreClosed() throws Exception {
@ParameterizedTest(name = "graceful? => {0}")
@ValueSource(booleans = {true, false})
void blockingRpcMethodsAreClosed(final boolean param) throws Exception {
setUp(param);
CloseSignal signal = new CloseSignal(4);
BlockingTestRpc testRpc = setupBlockingCloseMock(mock(BlockingTestRpc.class), signal);
BlockingTestRequestStreamRpc testRequestStreamRpc =
Expand All @@ -134,8 +128,10 @@ public void blockingRpcMethodsAreClosed() throws Exception {
signal.verifyClose(closeGracefully);
}

@Test
public void mixedModeRpcMethodsAreClosed() throws Exception {
@ParameterizedTest(name = "graceful? => {0}")
@ValueSource(booleans = {true, false})
void mixedModeRpcMethodsAreClosed(final boolean param) throws Exception {
setUp(param);
CloseSignal signal = new CloseSignal(4);
TestRpc testRpc = setupCloseMock(mock(TestRpc.class), signal);
TestRequestStreamRpc testRequestStreamRpc = setupCloseMock(mock(TestRequestStreamRpc.class), signal);
Expand Down
Loading

0 comments on commit 8ac828c

Please sign in to comment.