如何使用mockmvc进行错误处理测试?

问题描述 投票:0回答:1

我有一个方法和方法包括一个例外,如果一些param不写入url。我们将在下面展示

if (url != null) {
File = getService().getByUrl(url);
} else {
throw new IllegalStateException("you must send id);
}

这是我的方法对,我想编写异常测试。如何使用mockmvc进行cath错误消息?

return getMockMvc().perform(get(url))
.contentType(MediaType.APPLICATION_JSON_UTF8)
.andExpect(status().is5xxServerError());

此代码没有收到错误消息。你能帮助我吗

java exception junit5 mockmvc
1个回答
0
投票

你可以这样做:

return getMockMvc().perform(get(url)) .contentType(MediaType.APPLICATION_JSON_UTF8) .andExpect(status().is5xxServerError())
andExpect(content().string(containsString("you must send id")));
© www.soinside.com 2019 - 2024. All rights reserved.