This follows the issue Improve documentation for uri(URI) method in WebTestClient regarding base URI where all details are explained.

To summarize:

  • Using WebTestClient without @AutoConfigureWebTestClient and with @SpringBootTest(webEnvironment = RANDOM_PORT), make the integration tests not working without the base URI (Error: Connection refused).
  • Using WebTestClient with @AutoConfigureWebTestClient and @SpringBootTest(webEnvironment = RANDOM_PORT), make the tests work without the base URI.

I think this is not expected and it's not coherent from a developer prospective or at least need to be documented. See the link above for more details.

What I expect is that this code:

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("test")
@Sql("classpath:sql/my_sql.sql")
@Import(TestConfig.class)
        URI uri = UriComponentsBuilder
                .fromUriString(MY_URL)
                .queryParam("year", 2005)
                .buildAndExpand()
                .toUri();

        client.get()
                .uri(uri)
                .exchange()
                .expectStatus()
                .is2xxSuccessful();

has the same behaviour with the @AutoConfigureWebTestClient since @SpringBootTest by default configures a WebTestClient

Comment From: bclozel

I think spring-projects/spring-framework#28058 makes it clear that a complete URL is expected when using the URI method variants. This was really the core of the problem you've reported.

Now when it comes to the addition of @AutoConfigureWebTestClient, this "fixes" the problem in the sense that instead of sending actual HTTP requests over the wire, this will bind directly the WebTestClient to the web framework components: this means that running a live server is useless and makes it not different than a @SpringBootTest(webEnvironment = MOCK) test.

I think that we should better document this on the @AutoConfigureWebTestClient annotation itself as well as in this section of the reference documentation.

In your case, I think the following would be a better solution:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class WebtestclientApplicationTests {

    @Test
    void greetingWorks(@Autowired WebTestClient webTestClient, @LocalServerPort int port) {
        URI uri = UriComponentsBuilder
                .fromHttpUrl("http://localhost:{port}/hello/{name}")
                .buildAndExpand(port, "Spring")
                .toUri();

        webTestClient.get().uri(uri)
                .exchange().expectBody(String.class)
                .isEqualTo("Hello Spring!");
    }

}

Comment From: SimoneGiusso

Thanks for you explanation. I understand and I agree that the documentation should be improved in both sections. The tip in the section you linked make it clears that the @AutoConfigureWebTestClient could be used in both the situations but doesn't give any hint about the behaviour I encountered.

A better document should do the work and avoid this confusion. Thank you!

Comment From: SimoneGiusso

In your case, I think the following would be a better solution:

```java @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class WebtestclientApplicationTests {

@Test void greetingWorks(@Autowired WebTestClient webTestClient, @LocalServerPort int port) { URI uri = UriComponentsBuilder .fromHttpUrl("http://localhost:{port}/hello/{name}") .buildAndExpand(port, "Spring") .toUri();

  webTestClient.get().uri(uri)
          .exchange().expectBody(String.class)
          .isEqualTo("Hello Spring!");

}

} ```

Otherwise it would be simpler call .toUriString();:

        URI uri = UriComponentsBuilder
                .fromUriString(MY_URL)
                .queryParam("year", 2005)
                .buildAndExpand()
                .toUriString();

        client.get()
                .uri(uri) // in this case it will build the absolute URI
                .exchange()
                .expectStatus()
                .is2xxSuccessful();