理想情況下,我想要這個:
@Test
void testWelcome() throws Exception {
mockMvc.perform(get("/oups"))
.andExpect(status().isInternalServerError());
}
但是測驗失敗了,因為它在get("/oops")到達斷言之前就拋出了(運行一個拋出 RuntimeException 的控制器方法)。我該如何處理這個問題?這是我現在的快速修復:
@Test
void testTriggerException() throws Exception {
try {
mockMvc.perform(get("/oops"))
.andExpect(status().isInternalServerError());
} catch (Exception e) {
return;
}
fail();
}
uj5u.com熱心網友回復:
你可以試試這個:
Assertions
.assertThatThrownBy(
() -> mockMvc.perform(get("/oops").contentType(MediaType.APPLICATION_JSON)))
.hasCauseInstanceOf(RuntimeException.class)
.hasMessageContaining("The exception message");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/438150.html
