Class ExpectedSystemExit

java.lang.Object
org.junit.contrib.java.lang.system.ExpectedSystemExit
All Implemented Interfaces:
org.junit.rules.TestRule

public class ExpectedSystemExit extends Object implements org.junit.rules.TestRule
The ExpectedSystemExit allows in-test specification of expected System.exit(...) calls.

If your code calls System.exit(), then your test stops and doesn't finish. The ExpectedSystemExit rule allows in-test specification of expected System.exit() calls. Furthermore you cannot use JUnit's assert methods because of the abnormal termination of your code. As a substitute you can provide an Assertion object to the ExpectedSystemExit rule.

Some care must be taken if your system under test creates a new thread and this thread calls System.exit(). In this case you have to ensure that the test does not finish before System.exit() is called.

public class AppWithExit {
        public static String message;

        public static int doSomethingAndExit() {
                message = "exit ...";
                System.exit(1);
        }

        public static int doNothing() {
        }
}
public void AppWithExitTest {
  @Rule
  public final ExpectedSystemExit exit = ExpectedSystemExit.none();

  @Test
  public void exits() {
    exit.expectSystemExit();
    AppWithExit.doSomethingAndExit();
  }

  @Test
  public void exitsWithStatusCode1() {
    exit.expectSystemExitWithStatus(1);
    AppWithExit.doSomethingAndExit();
  }

  @Test
  public void writesMessage() {
    exit.checkAssertionAfterwards(new Assertion() {
      public void checkAssertion() {
        assertEquals("exit ...", AppWithExit.message);
      }
    });
    AppWithExit.doSomethingAndExit();
  }

  @Test
  public void systemExitWithStatusCode1() {
    exit.expectSystemExitWithStatus(1);
    AppWithExit.doSomethingAndExit();
  }

  @Test
  public void noSystemExit() {
    AppWithExit.doNothing();
    //passes
  }
}
  • Field Details

    • assertions

      private final Collection<Assertion> assertions
    • expectExit

      private boolean expectExit
    • expectedStatus

      private Integer expectedStatus
  • Constructor Details

    • ExpectedSystemExit

      private ExpectedSystemExit()
  • Method Details

    • none

      public static ExpectedSystemExit none()
    • expectSystemExitWithStatus

      public void expectSystemExitWithStatus(int status)
    • expectSystemExit

      public void expectSystemExit()
    • checkAssertionAfterwards

      public void checkAssertionAfterwards(Assertion assertion)
    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule
    • createNoExitSecurityManagerRule

      private ProvideSecurityManager createNoExitSecurityManagerRule()
    • createStatement

      private org.junit.runners.model.Statement createStatement(org.junit.runners.model.Statement base)
    • checkSystemExit

      private void checkSystemExit()
    • handleMissingSystemExit

      private void handleMissingSystemExit()
    • handleSystemExitWithStatus

      private void handleSystemExitWithStatus(int status)
    • checkAssertions

      private void checkAssertions() throws Exception
      Throws:
      Exception