JUnit testing System.exit: A small framework

doug
Fri, 04 Apr 2014 07:49:50 +0000

To add to my post on unit testing code that calls System.exit() I've wrote a little JUnit @Rule that allows you to set expectations in a simplified way.

The code is available on github.com/kay/assert-exit

Usage:

public class ExpectedExitTest {

  @Rule
  public ExpectedExit expectedExit = ExpectedExit.noAttempt();

  @Test
  public void invokesExitAsExpected() {
      this.expectedExit.expectExit(7);
      System.exit(7);
  }
}

Enjoy