小编ser*_*rat的帖子

Scala:如何测试调用System.exit()的方法?

我一直在开发一个命令行工具,它在某些输入上调用System.exit()(不想使用异常而不是).

我熟悉Java:如何测试调用System.exit()的方法?它是最优雅的方法.

不幸的是,它是不够纯,因为我不得不添加的依赖关系到系统的规则,JUnit的接口

System.exitspecs2中是否有任何常见的处理模式,这种模式比我目前使用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"});
    }
}
Run Code Online (Sandbox Code Playgroud)

unit-testing scala testability sbt specs2

9
推荐指数
1
解决办法
2125
查看次数

标签 统计

sbt ×1

scala ×1

specs2 ×1

testability ×1

unit-testing ×1