Skip to content
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

Closed
SimoneGiusso opened this issue Feb 16, 2022 · 3 comments
Labels
type: documentation A documentation update
Milestone

Comments

@SimoneGiusso
Copy link

SimoneGiusso commented Feb 16, 2022

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 16, 2022
@bclozel bclozel self-assigned this Feb 17, 2022
@bclozel
Copy link
Member

bclozel commented Feb 17, 2022

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!");
	}

}

@bclozel bclozel added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 17, 2022
@bclozel bclozel removed their assignment Feb 17, 2022
@bclozel bclozel added this to the 2.6.x milestone Feb 17, 2022
@bclozel bclozel changed the title Unexpected behaviour of WebTestClient in combination with @AutoConfigureWebTestClient and @SpringBootTest Document that @AutoConfigureWebTestClient binds WebTestClient to MOCK infrastructure Feb 17, 2022
@SimoneGiusso
Copy link
Author

SimoneGiusso commented Feb 17, 2022

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!

@SimoneGiusso
Copy link
Author

SimoneGiusso commented Feb 17, 2022

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!");
	}

}

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();

@wilkinsona wilkinsona modified the milestones: 2.6.x, 2.7.x Nov 24, 2022
@wilkinsona wilkinsona changed the title Document that @AutoConfigureWebTestClient binds WebTestClient to MOCK infrastructure Clarify that @AutoConfigureWebTestClient binds WebTestClient to mock infrastructure Oct 30, 2023
@wilkinsona wilkinsona modified the milestones: 2.7.x, 2.7.18 Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

4 participants