我一直在开发一个命令行工具,它在某些输入上调用System.exit()
(不想使用异常而不是)。
我熟悉Java:如何测试调用 System.exit() 的方法?它是最优雅的方法。
不幸的是,它不够纯净,因为我必须将依赖项添加到system-rules,junit-interface
System.exit
在specs2中是否有任何常见的处理模式,它比我目前不使用specs2的方法更纯粹?
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
public class ConverterTest {
@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
@Test
public void emptyArgs() {
exit.expectSystemExit();
Converter.main(new String[]{});
}
@Test
public void missingOutArgument() {
exit.expectSystemExitWithStatus(1);
Converter.main(new String[]{"--in", "src/test/resources/078.xml.gz"});
}
}