我有一个方法和方法包括一个例外,如果一些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());
此代码没有收到错误消息。你能帮助我吗
你可以这样做:
return getMockMvc().perform(get(url)) .contentType(MediaType.APPLICATION_JSON_UTF8) .andExpect(status().is5xxServerError())
andExpect(content().string(containsString("you must send id")));