Spring中如何测试无返回值方法

悬赏:5 发布时间:2008-08-08 提问人:wcleye (初级程序员)

项目使用 spring 的AbstractDependencyInjectionSpringContextTests + junit4 做单元测试,其中有的测试方法没有返回值,只抛出一个异常,如果方法执行失败会抛出这个异常,用 junit 时有一个 @Test(expected=xxxException.class),但是用了spring 的测试类后这个就不起作用了,那我应该如何完成这个测试呢?

采纳的答案

2008-08-08 小疯子 (资深程序员)

我们在测试中对异常的test是这样处理的:
public void testSomething() {
		try {
			service.execute(...);
			fail("Should throw a exception");
		} catch (xxxException e) {
			
		}

	}

如果不抛异常直接调用fail()方法!

提问者对于答案的评价:
谢啦.