Our integration tests in Spring Boot 3.2.0 via @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
and WebTestClient
fail with the following exception
org.springframework.web.reactive.function.client.WebClientRequestException:
Connection refused: no further information: /127.0.0.1:80
The test looks as follows:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestClass {
@Autowired
protected WebTestClient webTestClient;
@Test
void testShiftLog() {
// Given
URI uri = UriComponentsBuilder.fromUri(URI.create(SHIFTLOG_ENDPOINT_URL))
.build().toUri();
ShiftLogItem body = generateSchichtbuchItem();
// When, Then
webTestClient.post().uri(uri)
.body(Mono.just(body), ShiftLogItem.class)
.header("Content-Type", "application/json")
.exchange() // HERE THE EXCEPTION IS THROWN.
.expectStatus().isCreated()
.expectBody(Void.class);
}
}
When I debug the code I can see that the WebTestClient
object is configured correctly, namely to the random port. Still, the execption refers to localhost:80 what is totally odd. Any ideas what is wrong here?
The issue is solved now. The issue here is the
.uri(URI uri)
part. If you have a base URI applied as here then the.uri(URI uri)
will not be applied to the base URI but takes its place instead.So in order to fix it you must use the overloaded
.uri(String uri)
method. You can do it easily by URI'stoString()
method:See also the Spring Java Doc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/reactive/server/WebTestClient.UriSpec.html#uri(java.net.URI)