diff --git a/grpcmock-junit5/src/main/java/org/grpcmock/junit5/GrpcMockExtension.java b/grpcmock-junit5/src/main/java/org/grpcmock/junit5/GrpcMockExtension.java index 7042865..dc1a26d 100644 --- a/grpcmock-junit5/src/main/java/org/grpcmock/junit5/GrpcMockExtension.java +++ b/grpcmock-junit5/src/main/java/org/grpcmock/junit5/GrpcMockExtension.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.platform.commons.support.ModifierSupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,6 +74,9 @@ public void beforeAll(ExtensionContext extensionContext) { @Override public void afterAll(ExtensionContext extensionContext) { + if (extensionContext.getTestClass().map(this::isNestedClass).orElse(Boolean.FALSE)) { + return; + } server.resetAll(); server.stop(); log.debug("Stopping gRPC Mock server."); @@ -84,6 +88,10 @@ public void afterEach(ExtensionContext extensionContext) { log.debug("Resetting all stub mappings after a test"); } + private boolean isNestedClass(Class currentClass) { + return !ModifierSupport.isStatic(currentClass) && currentClass.isMemberClass(); + } + public static class Builder { private int port = 0; @@ -122,8 +130,8 @@ public Builder withInterceptor(@Nonnull ServerInterceptor interceptor) { } /** - * Defines certChain and privateKey files in order to configure server security via {@link - * ServerBuilder#useTransportSecurity(File, File)}. + * Defines certChain and privateKey files in order to configure server security via + * {@link ServerBuilder#useTransportSecurity(File, File)}. */ public Builder withTransportSecurity(@Nonnull File certChain, @Nonnull File privateKey) { Objects.requireNonNull(certChain); @@ -134,8 +142,8 @@ public Builder withTransportSecurity(@Nonnull File certChain, @Nonnull File priv } /** - * Defines certChain and privateKey files in order to configure server security via {@link - * ServerBuilder#useTransportSecurity(File, File)}. + * Defines certChain and privateKey files in order to configure server security via + * {@link ServerBuilder#useTransportSecurity(File, File)}. */ public Builder withTransportSecurity(@Nonnull String certChain, @Nonnull String privateKey) { Objects.requireNonNull(certChain); diff --git a/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/NestedTest.java b/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/NestedTest.java new file mode 100644 index 0000000..45ad73b --- /dev/null +++ b/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/NestedTest.java @@ -0,0 +1,31 @@ +package org.grpcmock.junit5.test; + +import org.grpcmock.junit5.GrpcMockExtension; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * @author Fadelis + */ +@ExtendWith(GrpcMockExtension.class) +class NestedTest extends TestBase { + + @Nested + class FirstNestedClass { + + @Test + void should_run_nested_test_1() { + runAndAssertSimpleHealthCheckRequest(); + } + } + + @Nested + class SecondNestedClass { + + @Test + void should_run_nested_test_2() { + runAndAssertSimpleHealthCheckRequest(); + } + } +} diff --git a/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/TestBase.java b/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/TestBase.java index fbf9663..7fd0463 100644 --- a/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/TestBase.java +++ b/grpcmock-junit5/src/test/java/org/grpcmock/junit5/test/TestBase.java @@ -38,7 +38,7 @@ void tearDownChannel() { void runAndAssertHealthCheckRequest(HealthCheckRequest request, HealthCheckResponse response) { HealthBlockingStub serviceStub = HealthGrpc.newBlockingStub(serverChannel); - assertThat(serviceStub.check(HealthCheckRequest.getDefaultInstance())).isEqualTo(response); + assertThat(serviceStub.check(request)).isEqualTo(response); } void runAndAssertSimpleHealthCheckRequest() {