我想声明引发异常并且服务器返回500内部服务器错误.
thrown.expect(nestedservletexception.class);
this.mockmvc.perform(post("/account")
.contentType(MediaType.APPLICATION_JSON)
.content(requestString))
.andExpect(status().isInternalServerError());
当然,如果我写了isInternalServerError或isOk,那也没关系.
无论是否在throw.except语句下面抛出异常,测试都将通过.
你会怎么解决这个问题?
最佳答案
您可以尝试以下方式 –
>创建自定义匹配器
public class CustomExceptionMatcher extends
TypeSafeMatcher4045@ion();
return actual.equals(expected);
}
@Override
public void describeto(Description desc) {
desc.appendText("Actual =").appendValue(actual)
.appendText(" Expected =").appendValue(
expected);
}
}
>在JUnit类中声明@Rule,如下所示 –
@Rule
public ExpectedException exception = ExpectedException.none();
>在测试用例中使用自定义匹配器 –
exception.expect(CustomException.class);
exception.expect(CustomException
.assertSomeThing("Some assertion text"));
this.mockmvc.perform(post("/account")
.contentType(MediaType.APPLICATION_JSON)
.content(requestString))
.andExpect(status().isInternalServerError());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。