Java: How to test methods that call System.exit()? Java: How to test methods that call System.exit()? multithreading multithreading

Java: How to test methods that call System.exit()?


Indeed, Derkeiler.com suggests:

  • Why System.exit() ?

Instead of terminating with System.exit(whateverValue), why not throw an unchecked exception? In normal use it will drift all the way out to the JVM's last-ditch catcher and shut your script down (unless you decide to catch it somewhere along the way, which might be useful someday).

In the JUnit scenario it will be caught by the JUnit framework, which will report that such-and-such test failed and move smoothly along to the next.

  • Prevent System.exit() to actually exit the JVM:

Try modifying the TestCase to run with a security manager that prevents calling System.exit, then catch the SecurityException.

public class NoExitTestCase extends TestCase {    protected static class ExitException extends SecurityException     {        public final int status;        public ExitException(int status)         {            super("There is no escape!");            this.status = status;        }    }    private static class NoExitSecurityManager extends SecurityManager     {        @Override        public void checkPermission(Permission perm)         {            // allow anything.        }        @Override        public void checkPermission(Permission perm, Object context)         {            // allow anything.        }        @Override        public void checkExit(int status)         {            super.checkExit(status);            throw new ExitException(status);        }    }    @Override    protected void setUp() throws Exception     {        super.setUp();        System.setSecurityManager(new NoExitSecurityManager());    }    @Override    protected void tearDown() throws Exception     {        System.setSecurityManager(null); // or save and restore original        super.tearDown();    }    public void testNoExit() throws Exception     {        System.out.println("Printing works");    }    public void testExit() throws Exception     {        try         {            System.exit(42);        } catch (ExitException e)         {            assertEquals("Exit status", 42, e.status);        }    }}

Update December 2012:

Will proposes in the comments using System Rules, a collection of JUnit(4.9+) rules for testing code which uses java.lang.System.
This was initially mentioned by Stefan Birkner in his answer in December 2011.

System.exit(…)

Use the ExpectedSystemExit rule to verify that System.exit(…) is called.
You could verify the exit status, too.

For instance:

public void MyTest {    @Rule    public final ExpectedSystemExit exit = ExpectedSystemExit.none();    @Test    public void noSystemExit() {        //passes    }    @Test    public void systemExitWithArbitraryStatusCode() {        exit.expectSystemExit();        System.exit(0);    }    @Test    public void systemExitWithSelectedStatusCode0() {        exit.expectSystemExitWithStatus(0);        System.exit(0);    }}


The library System Lambda has a method catchSystemExit.With this rule you are able to test code, that calls System.exit(...):

public class MyTest {    @Test    public void systemExitWithArbitraryStatusCode() {        SystemLambda.catchSystemExit(() -> {            //the code under test, which calls System.exit(...);        });    }    @Test    public void systemExitWithSelectedStatusCode0() {        int status = SystemLambda.catchSystemExit(() -> {            //the code under test, which calls System.exit(0);        });        assertEquals(0, status);    }}

For Java 5 to 7 the library System Rules has a JUnit rule called ExpectedSystemExit. With this rule you are able to test code, that calls System.exit(...):

public class MyTest {    @Rule    public final ExpectedSystemExit exit = ExpectedSystemExit.none();    @Test    public void systemExitWithArbitraryStatusCode() {        exit.expectSystemExit();        //the code under test, which calls System.exit(...);    }    @Test    public void systemExitWithSelectedStatusCode0() {        exit.expectSystemExitWithStatus(0);        //the code under test, which calls System.exit(0);    }}

Full disclosure: I'm the author of both libraries.


How about injecting an "ExitManager" into this Methods:

public interface ExitManager {    void exit(int exitCode);}public class ExitManagerImpl implements ExitManager {    public void exit(int exitCode) {        System.exit(exitCode);    }}public class ExitManagerMock implements ExitManager {    public bool exitWasCalled;    public int exitCode;    public void exit(int exitCode) {        exitWasCalled = true;        this.exitCode = exitCode;    }}public class MethodsCallExit {    public void CallsExit(ExitManager exitManager) {        // whatever        if (foo) {            exitManager.exit(42);        }        // whatever    }}

The production code uses the ExitManagerImpl and the test code uses ExitManagerMock and can check if exit() was called and with which exit code.