[JAVA-12608] Add logging to debug failing tests (#12587)

This commit is contained in:
Haroon Khan 2022-08-12 16:02:30 +01:00 committed by GitHub
parent 3252ba5775
commit a800f72b99
2 changed files with 34 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 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; import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@Slf4j
class WebClientIntegrationTest { class WebClientIntegrationTest {
private static final String BODY_VALUE = "bodyValue"; private static final String BODY_VALUE = "bodyValue";
@ -54,11 +56,14 @@ class WebClientIntegrationTest {
@Test @Test
void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() { void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() {
log.info("Executing test using several webclient configs ....");
// WebClient creation // WebClient creation
WebClient client1 = WebClient.create(); WebClient client1 = WebClient.builder().clientConnector(httpConnector()).build();
WebClient client2 = WebClient.create("http://localhost:" + port); WebClient client2 = WebClient.builder().baseUrl("http://localhost:" + port)
.clientConnector(httpConnector()).build();
WebClient client3 = WebClient.builder() WebClient client3 = WebClient.builder()
.baseUrl("http://localhost:" + port) .baseUrl("http://localhost:" + port)
.clientConnector(httpConnector())
.defaultCookie("cookieKey", "cookieValue") .defaultCookie("cookieKey", "cookieValue")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080")) .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
@ -75,21 +80,30 @@ class WebClientIntegrationTest {
StepVerifier.create(retrieveResponse(client3)) StepVerifier.create(retrieveResponse(client3))
.expectNext("processed-bodyValue") .expectNext("processed-bodyValue")
.verifyComplete(); .verifyComplete();
// assert response without specifying URI // assert response without specifying URI
log.info("Obtaining error response using default client (without url) ....");
StepVerifier.create(retrieveResponse(client1)) StepVerifier.create(retrieveResponse(client1))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused")) .contains("Connection refused"))
.log()
.verify(); .verify();
log.info("error response processed as expected");
} }
@Test @Test
void givenWebClientCreationWithoutUri_whenUsed_thenObtainExpectedResponse() { 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)) StepVerifier.create(retrieveResponse(client))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused")) .contains("Connection refused"))
.log()
.verify(); .verify();
log.info("test completed....");
} }
@Test @Test
@ -137,16 +151,19 @@ class WebClientIntegrationTest {
@Test @Test
void givenOverriddenUriSpecifications_whenUsed_thenObtainExpectedResponse() { void givenOverriddenUriSpecifications_whenUsed_thenObtainExpectedResponse() {
RequestBodySpec bodySpecOverriddenBaseUri = createDefaultPostRequest().uri(URI.create("/resource")); RequestBodySpec bodySpecOverriddenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri)) StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused")) .contains("Connection refused"))
.verify(); .verify();
RequestBodySpec bodySpecOverriddenBaseUri2 = WebClient.builder() RequestBodySpec bodySpecOverriddenBaseUri2 = WebClient.builder()
.clientConnector(httpConnector())
.baseUrl("http://localhost:" + port) .baseUrl("http://localhost:" + port)
.build() .build()
.post() .post()
.uri(URI.create("/resource")); .uri(URI.create("/resource"));
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri2)) StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri2))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused")) .contains("Connection refused"))
@ -294,7 +311,17 @@ class WebClientIntegrationTest {
// helper methods to create default instances // helper methods to create default instances
private WebClient createDefaultClient() { 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() { private RequestBodyUriSpec createDefaultPostRequest() {

View File

@ -9,6 +9,9 @@
name="com.baeldung.reactive.debugging.consumer.service.FooService"> name="com.baeldung.reactive.debugging.consumer.service.FooService">
<appender-ref ref="LISTAPPENDER" /> <appender-ref ref="LISTAPPENDER" />
</logger> </logger>
<logger name="reactor.netty.http.client" level="DEBUG"/>
<root level="info"> <root level="info">
<appender-ref ref="CONSOLE" /> <appender-ref ref="CONSOLE" />
<appender-ref ref="LISTAPPENDER" /> <appender-ref ref="LISTAPPENDER" />