簡體   English   中英

如何解決來自 WebTestClient 的異常?

[英]How to resolve the Exception from WebTestClient?

如何使用WebTestClient實現以下目標?

@Autowired
private MockMvc mvc;

mvc.perform(req)
        .andExpect(status)
        .andReturn().getResolvedException();

這完全不一樣,我如何才能真正解決Exception

@Autowired
private WebTestClient webTestClient;

webTestClient.post()
       .exchange()
       .returnResult(String.class)
       .getResponseBody();

雖然以下方法有效,但我不知道這是否是正確的方法:

...
.expectBody()
.consumeWith(res -> {
    Exception ex = ((MvcResult) res.getMockServerResult()).getResolvedException();
    assertEquals(ex instanceof MyException.class);
    assertEquals("Hello Exception", ex.getMessage());
})

請注意,這僅在對類路徑具有spring-web依賴項時才有效。 只有spring-webflux ,這將失敗,因為.getMockServerResult()總是為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM