From dc6e305e1fa6c769dd2f6898ea014e162b6c2084 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Fri, 12 Feb 2021 20:08:12 -0300 Subject: [PATCH] fixed potential error using webClient without specifying url, and improved timeout test case --- .../web/client/WebClientIntegrationTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java index 39adf0b5c0..d7c31d627a 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebClientIntegrationTest.java @@ -11,8 +11,6 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; -import org.reactivestreams.Publisher; -import org.reactivestreams.Subscriber; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.web.server.LocalServerPort; @@ -41,6 +39,7 @@ import com.baeldung.web.reactive.client.Foo; import com.baeldung.web.reactive.client.WebClientApplication; import io.netty.channel.ChannelOption; +import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler; import reactor.core.publisher.Mono; @@ -265,17 +264,17 @@ public class WebClientIntegrationTest { .addHandlerLast(new WriteTimeoutHandler(1000, TimeUnit.MILLISECONDS))); WebClient timeoutClient = WebClient.builder() + .baseUrl("http://localhost:" + port) .clientConnector(new ReactorClientHttpConnector(httpClient)) .build(); - BodyInserter, ReactiveHttpOutputMessage> inserterCompleteSuscriber = BodyInserters.fromPublisher(Subscriber::onComplete, String.class); - RequestHeadersSpec headersSpecInserterCompleteSuscriber = timeoutClient.post() + StepVerifier.create(timeoutClient.post() .uri("/resource") - .body(inserterCompleteSuscriber); - - StepVerifier.create(headersSpecInserterCompleteSuscriber.retrieve() + .body(Mono.never(), String.class) + .retrieve() .bodyToMono(String.class)) - .expectTimeout(Duration.ofMillis(2000)) + .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ReadTimeoutException.class.isAssignableFrom(ex.getCause() + .getClass())) .verify(); }