-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Streamline WebTestClient creation and customization in integration tests #15132
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
Comments
I did not have success with I tried autowiring I did come up with the following: @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebTestClient
public class LocationServiceTests {
@Autowired
private WebTestClient client;
@Before
public void setup() {
DefaultUriBuilderFactory builderFactory = new DefaultUriBuilderFactory();
builderFactory.setEncodingMode(URI_COMPONENT);
this.client = this.client.mutate().uriBuilderFactory(builderFactory).build();
}
@Test
public void exampleTest() {
this.client.get().uri("/").exchange().expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello World");
}
} |
@rstoyanchev, your proposal does not apply case That is why I wrote The only way I found is drop autoconfigured webclient and create custom one. |
Oh, I see now. You want to customize the UriBuilderFactory but that overrides the baseUrl set up by Boot. Hm, I'm not sure I see a good way to do this. Effectively both the test code and Boot are competing to customize the UriBuilderFactory. |
The @TestConfiguration
static class BuilderCustomizerConfiguration {
@Bean
WebTestClientBuilderCustomizer customizer() {
return (builder) -> {
builder.filter((request, next) -> {
System.out.println(request.method() + " " + request.url());
return next.exchange(request);
});
};
}
} This is an imperfect solution, though. Using I think this is more than a documentation issue as I don't think we have a particularly good solution to document at the moment. |
When a test is annotated with
There are some other difference in Two possible options for
Of the two, the second seems the better to me. The first is problematic as, due to |
Here's a proposal for the team's consideration. It implements the second of the two options listed above. It still limits support to a server bound |
The docs show an example of using the
WebTestClient
but nothing to show how to customize its configuration like this section does for theWebClient
. I did findWebTestClientBuilderCustomizer
but that should be explained in the reference and if there are other options, those could be mentioned too.This is based on related comment under SPR-17039.
The text was updated successfully, but these errors were encountered: