From a800f72b99e2c6dd7de694422b87c96a465fe872 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 12 Aug 2022 16:02:30 +0100 Subject: [PATCH] [JAVA-12608] Add logging to debug failing tests (#12587) --- .../webclient/WebClientIntegrationTest.java | 35 ++++++++++++++++--- .../src/test/resources/logback-test.xml | 3 ++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/spring-5-reactive-modules/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebClientIntegrationTest.java b/spring-5-reactive-modules/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebClientIntegrationTest.java index 5f42610c3d..d9bb966def 100644 --- a/spring-5-reactive-modules/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebClientIntegrationTest.java +++ b/spring-5-reactive-modules/spring-reactive/src/test/java/com/baeldung/reactive/webclient/WebClientIntegrationTest.java @@ -4,6 +4,7 @@ import io.netty.channel.ChannelOption; import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -43,6 +44,7 @@ import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) +@Slf4j class WebClientIntegrationTest { private static final String BODY_VALUE = "bodyValue"; @@ -54,11 +56,14 @@ class WebClientIntegrationTest { @Test void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() { + log.info("Executing test using several webclient configs ...."); // WebClient creation - WebClient client1 = WebClient.create(); - WebClient client2 = WebClient.create("http://localhost:" + port); + WebClient client1 = WebClient.builder().clientConnector(httpConnector()).build(); + WebClient client2 = WebClient.builder().baseUrl("http://localhost:" + port) + .clientConnector(httpConnector()).build(); WebClient client3 = WebClient.builder() .baseUrl("http://localhost:" + port) + .clientConnector(httpConnector()) .defaultCookie("cookieKey", "cookieValue") .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080")) @@ -75,21 +80,30 @@ class WebClientIntegrationTest { StepVerifier.create(retrieveResponse(client3)) .expectNext("processed-bodyValue") .verifyComplete(); + // assert response without specifying URI + log.info("Obtaining error response using default client (without url) ...."); StepVerifier.create(retrieveResponse(client1)) .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .contains("Connection refused")) + .log() .verify(); + log.info("error response processed as expected"); } @Test void givenWebClientCreationWithoutUri_whenUsed_thenObtainExpectedResponse() { - WebClient client = WebClient.create(); + log.info("Executing test for connection refused...."); + WebClient client = WebClient.builder() + .clientConnector(httpConnector()) + .build(); StepVerifier.create(retrieveResponse(client)) .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .contains("Connection refused")) + .log() .verify(); + log.info("test completed...."); } @Test @@ -137,16 +151,19 @@ class WebClientIntegrationTest { @Test void givenOverriddenUriSpecifications_whenUsed_thenObtainExpectedResponse() { RequestBodySpec bodySpecOverriddenBaseUri = createDefaultPostRequest().uri(URI.create("/resource")); + StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri)) .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .contains("Connection refused")) .verify(); RequestBodySpec bodySpecOverriddenBaseUri2 = WebClient.builder() + .clientConnector(httpConnector()) .baseUrl("http://localhost:" + port) .build() .post() .uri(URI.create("/resource")); + StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri2)) .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .contains("Connection refused")) @@ -294,7 +311,17 @@ class WebClientIntegrationTest { // helper methods to create default instances private WebClient createDefaultClient() { - return WebClient.create("http://localhost:" + port); + return WebClient.builder() + .baseUrl("http://localhost:" + port) + .clientConnector(httpConnector()) + .build(); + } + + private static ReactorClientHttpConnector httpConnector() { + HttpClient httpClient = HttpClient + .create() + .wiretap(true); + return new ReactorClientHttpConnector(httpClient); } private RequestBodyUriSpec createDefaultPostRequest() { diff --git a/spring-5-reactive-modules/spring-reactive/src/test/resources/logback-test.xml b/spring-5-reactive-modules/spring-reactive/src/test/resources/logback-test.xml index 69c10177a9..f9bb83dbcd 100644 --- a/spring-5-reactive-modules/spring-reactive/src/test/resources/logback-test.xml +++ b/spring-5-reactive-modules/spring-reactive/src/test/resources/logback-test.xml @@ -9,6 +9,9 @@ name="com.baeldung.reactive.debugging.consumer.service.FooService"> + + +