库系统规则具有一个称为
ExpectedSystemExit的JUnit规则。使用此规则,你可以测试调用
System.exit(...)的代码:
public void MyTest { @Rule public final ExpectedSystemExit exit = ExpectedSystemExit.none(); @Test public void systemExitWithArbitraryStatusCode() { exit.expectSystemExit(); //the pre under test, which calls System.exit(...); } @Test public void systemExitWithSelectedStatusCode0() { exit.expectSystemExitWithStatus(0); //the pre under test, which calls System.exit(0); }}


