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

时间:2019-04-01 12:16:55

标签: java exception junit5 mockmvc

我有一种方法,如果某些参数未写在url中,则该方法包括一个异常。让我们在下面显示

df1 = pd.DataFrame(a.T)

df['new_col'] = df1.iloc[-1] - df1.iloc[0]

print(df)

   num letter  new_col
0    1      a        6
1    2      b       30
2    3      c       12

这是我的方法对,我想编写异常测试。如何使用Mockmvc发出错误消息?

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

此代码未显示错误消息。你能帮我吗

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

return getMockMvc().perform(get(url)) .contentType(MediaType.APPLICATION_JSON_UTF8) .andExpect(status().is5xxServerError())
andExpect(content().string(containsString("you must send id")));