在junit类中,我想测试抛出异常,如果没有,则提供一条解释性消息,建议在测试失败时查找什么 . 所以,像......
thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("an exception message");
thrown.failureMessage("This method should not be called directly. Be sure to use a subclass or mock that implements the method foo()");
显然,没有这样的方法failureMessage() . 我不可能是世界上唯一一个想要实现失败信息的人 . 我的搜索中没有找到任何示例 .
请注意,这是参考Junit4 @Rule和ExpectedException功能 .
2 回答
JUnit 4.12支持自定义失败消息(请参阅release notes) . 如果您的测试没有抛出预期的异常,则使用此失败消息 . Hamcrest匹配器处理其他所有错误信息 .
JUnit 4.12于2014-12-04发布 .
您可以使用自定义
Matcher
,子类化BaseMatcher
覆盖toString()
来执行您想要的操作 .