特定の入力に対してSystem.exit()
(の代わりに例外を使用したくない)を呼び出すコマンドライン ツールを開発しています。
私はJava に精通しています: System.exit() を呼び出すメソッドをテストするには? そしてその最もエレガントなアプローチです。
残念ながら、依存関係をsystem-rules、junit-interfaceに追加する必要があったため、純粋ではありません。
specs2を使用しない現在のアプローチよりも純粋なspecs2System.exit
で処理するための一般的なパターンはありますか?
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"});
}
}