我一直在开发一个命令行工具,它在特定输入时调用 System.exit()
(不想使用异常)。
我熟悉 Java:如何测试调用 System.exit() 的方法?及其最优雅的 方法。
不幸的是,这并不够纯粹,因为我不得不添加依赖项到 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"});
}
}