如何使用WebTestClient实现以下功能
@Autowired
private MockMvc mvc;
mvc.perform(req)
.andExpect(status)
.andReturn().getResolvedException();这是完全不一样的,我如何才能实际解决Exception
@Autowired
private WebTestClient webTestClient;
webTestClient.post()
.exchange()
.returnResult(String.class)
.getResponseBody();发布于 2022-02-14 15:36:34
虽然以下方法有效,但我不知道这样做是否正确:
...
.expectBody()
.consumeWith(res -> {
Exception ex = ((MvcResult) res.getMockServerResult()).getResolvedException();
assertEquals(ex instanceof MyException.class);
assertEquals("Hello Exception", ex.getMessage());
})注意,只有在类路径上具有spring-web依赖关系时,这才有效。如果只有spring-webflux,这将失败,因为.getMockServerResult()总是空的。
https://stackoverflow.com/questions/71113248
复制相似问题