I´m currently having nightmares testing my Spring WebClient code. Given the following simple method
private String request(String uri, String body) {
return webClient
.method(HttpMethod.POST)
.uri(uri)
.body(Mono.just(body), String.class)
.retrieve()
.bodyToMono(String.class)
.block();
}
and the following Unit Test:
@Before
public void setUp() throws IOException {
baseUrl = "http://localhost:%s";
mockWebServer = new MockWebServer();
mockWebServer.start();
int mockWebServerPort = mockWebServer.getPort();
baseUrl = String.format(baseUrl, mockWebServerPort);
webClient = WebClient.create();
}
@After
public void shutdown() throws IOException {
mockWebServer.shutdown();
}
@Test
public void testPost() throws InterruptedException {
String body = "Hello";
givenMockResponse(response -> {
response.setResponseCode(200);
response.setBody(body);
});
request(baseUrl, body);
RecordedRequest recordedRequest = mockWebServer.takeRequest();
checkRequest(recordedRequest);
}
It passes when I run the test via IntelliJ but when I´m using maven it throws the following exception:
reactor.core.Exceptions$ReactiveException: java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 0ms in 'flatMap' (and no fallback has been configured)
The request method also runs fine when I start my Spring Boot Application and test it manually. My project uses Spring Boot 2.3.1, the maven-surefire-plugin:3.0.0-M3, JDK 11 and for testing JUnit4 with the okhttp3 mockwebserver 4.0.1. Do you have any idea what this problem could be caused by ?