-
Notifications
You must be signed in to change notification settings - Fork 40.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify that @AutoConfigureWebTestClient binds WebTestClient to mock infrastructure #29890
Comments
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 I think that we should better document this on the 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!");
}
} |
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 A better document should do the work and avoid this confusion. Thank you! |
Otherwise it would be simpler call 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(); |
This follows the issue Improve documentation for uri(URI) method in WebTestClient regarding base URI where all details are explained.
To summarize:
@AutoConfigureWebTestClient
and with@SpringBootTest(webEnvironment = RANDOM_PORT)
, make the integration tests not working without the base URI (Error: Connection refused).@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:
has the same behaviour with the
@AutoConfigureWebTestClient
since@SpringBootTest
by default configures a WebTestClientThe text was updated successfully, but these errors were encountered: