1
可能重复:
Java: How to test methods that call System.exit()?如何测试在Java中调用System.exit()的方案?
在某些情况下,我想测试的应用程序进行功能调用发送一封电子邮件,然后调用System.exit(0)
这是一个Java应用程序,并且Mockito被用于嘲笑。
我的测试目前看起来是这样的:
testSendsEmailInScenario() {
// set up
foo.bar(mock);
ArgumentCaptor<HashMap> argument = ArgumentCaptor.forClass(HashMap.class);
verify(mockEmailDeliveryManager).send(argument.capture());
Map<String,String> argMap = argument.getValue();
// Test that map contains the right stuff
}
所以这是不行的,因为应用程序中调用send
后,System.exit()
被调用,这会终止测试没有测试成功两种或失败。
由于exit()
是一个System
的静态方法,我不能用Mockito来嘲笑它。那么,我该如何:
- 为了本测试的目的,暂停退出的行为。
- 编写第二个测试,确认退出发生。 (只有调用
System.exit()
才会成功,否则失败。)
有趣,但我希望不要引入新的依赖关系。不过我会记住它的。 – 2011-06-13 21:00:25