项目使用 spring 的AbstractDependencyInjectionSpringContextTests + junit4 做单元测试,其中有的测试方法没有返回值,只抛出一个异常,如果方法执行失败会抛出这个异常,用 junit 时有一个 @Test(expected=xxxException.class),但是用了spring 的测试类后这个就不起作用了,那我应该如何完成这个测试呢?
我们在测试中对异常的test是这样处理的:
[code="java"]public void testSomething() {
try {
service.execute(...);
fail("Should throw a exception");
} catch (xxxException e) {
}
}[/code]
如果不抛异常直接调用fail()方法!