Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Indeed, <a href="http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2008-04/msg02603.html" rel="noreferrer">Derkeiler.com</a> suggests:</p> <ul> <li>Why <code>System.exit()</code> ?</li> </ul> <blockquote> <p>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). </p> <p>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.</p> </blockquote> <ul> <li>Prevent <code>System.exit()</code> to actually exit the JVM:</li> </ul> <blockquote> <p>Try modifying the TestCase to run with a security manager that prevents calling System.exit, then catch the SecurityException.</p> </blockquote> <pre><code>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); } } } </code></pre> <hr> <p>Update December 2012:</p> <p><a href="https://stackoverflow.com/users/557117/will">Will</a> proposes <a href="https://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit/309427#comment19186122_309427">in the comments</a> using <a href="http://stefanbirkner.github.com/system-rules/" rel="noreferrer"><strong>System Rules</strong></a>, a collection of JUnit(4.9+) rules for testing code which uses <code>java.lang.System</code>.<br> This was initially mentioned by <strong><a href="https://stackoverflow.com/users/557091/stefan-birkner">Stefan Birkner</a></strong> in <a href="https://stackoverflow.com/a/8658497/6309">his answer</a> in December 2011.</p> <pre><code>System.exit(…) </code></pre> <blockquote> <p>Use the <a href="http://stefanbirkner.github.com/system-rules/#ExpectedSystemExit" rel="noreferrer"><code>ExpectedSystemExit</code></a> rule to verify that <code>System.exit(…)</code> is called.<br> You could verify the exit status, too.</p> </blockquote> <p>For instance:</p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload