It would be great to access the MvcResult
in the WebTestClient
validation flow.
Currently, if you want to validate eg. the exception message and type, you have to:
webTestClient.post()
...
.expectBody()
.consumeWith(rsp -> {
MvcResult mvc = ((MvcResult) rsp.getMockServerResult());
assertEquals("expected message", mvc.getResolvedException().getMessage());
assertTrue(mvc.getResolvedException() instanceof MyCustomException);
});
It would be great if that could just be integrated and asserted inside the builder flow, if possible?
Comment From: bclozel
I think you're looking for MockMvcWebTestClient#resultActionsFor
:
FluxExchangeResult<Person> result = webTestClient.post().uri("/people/123")
.exchange().returnResult(Person.class);
MockMvcWebTestClient.resultActionsFor(result).andExpect(mvcResult ->
assertThat(mvcResult.getResolvedException()).hasMessageContaining("expected message").isInstanceOf(MyCustomException.class));
Does this work?
Comment From: membersound
While this might work, it would still be better if you could write mvcResult
assertions directly in the WebTestClient
builder method flow, like .exchange().expect(mvcResult -> assertThat(mvcResult.getResolvedException()).hasMessageContaining("expected message").isInstanceOf(MyCustomException.class));
Comment From: bclozel
MockMvcWebTestClient
transitively imports spring-webmvc classes, whereas WebTestClient
has been designed initially for WebFlux usage. Implementing your suggestion would make Spring MVC a requirement here. This is not a change we want to introduce.
I'm closing this issue as a result.
Comment From: membersound
Then the migration docs are a bit mislead, as they pretend that WebTestClient
is a direct replacement for MockMvc
:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#using-webtestclient-for-testing-spring-mvc
This change also enables WebTestClient for Spring MVC in mock environments: classes annotated with @AutoConfigureMockMvc can get injected a WebTestClient. This makes our support complete, you can now use a single API to drive all your web tests!
And I think being able to test with a single API, as suggested, would be indeed the best way.
Comment From: bclozel
WebTestClient
is focusing mainly on HTTP messages. To me, this sentence means that you can use the same client for mock and live server integration tests. MockMvcWebTestClient
provides a lot of additional features, but MockMvc
is definitely not deprecated, so keeping it in your test suite is perfectly fine.
Comment From: membersound
I can confirm that .resultActionsFor()
is sufficient to support some validation actions on MockMvc that are not possible inside the WebTestClient builder flow.