【发布时间】:2016-08-22 18:52:20
【问题描述】:
我一直在开发一个命令行工具,它在某些输入上调用System.exit()(不想使用异常而不是)。
我熟悉Java: How to test methods that call System.exit()? 和它最优雅的approach。
不幸的是,它不够纯,由于我必须将依赖项添加到system-rules,junit-interface
在 specs2 中处理System.exit 是否有任何通用模式,比我目前不使用 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"});
}
}
【问题讨论】:
-
查看groups.google.com/forum/#!topic/scalatest-users/pyKHtcP6HXM,Bill Venners(ScalaTest 的作者)提供了一个有用的答案。
标签: scala unit-testing sbt specs2 testability