Skip to content

Commit

Permalink
skip afterAll callback for nexted classes, closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Fadelis committed Sep 19, 2022
1 parent bb7d430 commit 418c8ea
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.");
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 418c8ea

Please sign in to comment.