<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.theeggeadventure.com/wikimedia/index.php?action=history&amp;feed=atom&amp;title=How_to_test_System.exit</id>
	<title>How to test System.exit - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.theeggeadventure.com/wikimedia/index.php?action=history&amp;feed=atom&amp;title=How_to_test_System.exit"/>
	<link rel="alternate" type="text/html" href="https://www.theeggeadventure.com/wikimedia/index.php?title=How_to_test_System.exit&amp;action=history"/>
	<updated>2026-05-16T00:04:07Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0-beta</generator>
	<entry>
		<id>https://www.theeggeadventure.com/wikimedia/index.php?title=How_to_test_System.exit&amp;diff=1729&amp;oldid=prev</id>
		<title>Egge at 02:33, 1 October 2007</title>
		<link rel="alternate" type="text/html" href="https://www.theeggeadventure.com/wikimedia/index.php?title=How_to_test_System.exit&amp;diff=1729&amp;oldid=prev"/>
		<updated>2007-10-01T02:33:28Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw-interface=&quot;&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 02:33, 1 October 2007&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l72&quot;&gt;Line 72:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 72:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Java]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key egge_wikimedia-mw_:diff:1.41:old-1556:rev-1729:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Egge</name></author>
	</entry>
	<entry>
		<id>https://www.theeggeadventure.com/wikimedia/index.php?title=How_to_test_System.exit&amp;diff=1556&amp;oldid=prev</id>
		<title>Brianegge at 03:19, 20 March 2007</title>
		<link rel="alternate" type="text/html" href="https://www.theeggeadventure.com/wikimedia/index.php?title=How_to_test_System.exit&amp;diff=1556&amp;oldid=prev"/>
		<updated>2007-03-20T03:19:19Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;One might assume that testing a console app method which calls System.exit() would be impossible to do.  By creating a SecurityManager we can detect and defer this call.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.security.Permission;&lt;br /&gt;
import java.util.Stack;&lt;br /&gt;
&lt;br /&gt;
public class ConsoleAppUnitTest extends UnitTestCase {&lt;br /&gt;
    private SecurityManager existingSecurityManager;&lt;br /&gt;
&lt;br /&gt;
    @Override&lt;br /&gt;
    protected void setUp() throws Exception {&lt;br /&gt;
        existingSecurityManager = System.getSecurityManager();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Override&lt;br /&gt;
    protected void tearDown() throws Exception {&lt;br /&gt;
        System.setSecurityManager(existingSecurityManager);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    private static class ExitCatchingSecurityManager extends SecurityManager {&lt;br /&gt;
        private Stack&amp;lt;Integer&amp;gt; exitCodes = new Stack&amp;lt;Integer&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
        public void checkExit(int status) {&lt;br /&gt;
            exitCodes.push(status);&lt;br /&gt;
            throw new SecurityException();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
         * This method is called before the original security manager can be replaced.  If this method does&lt;br /&gt;
         * not throw and exception then &amp;#039;System.setSecurityManager(existingSecurityManager)&amp;#039; will work.&lt;br /&gt;
         * @param perm anything&lt;br /&gt;
         */&lt;br /&gt;
        @Override&lt;br /&gt;
        public void checkPermission(Permission perm) {&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        public int getLastExit() {&lt;br /&gt;
            return exitCodes.pop();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void testNoArgsExitsWithReturnCode() {&lt;br /&gt;
        ExitCatchingSecurityManager securityManager = new ExitCatchingSecurityManager();&lt;br /&gt;
        try {&lt;br /&gt;
            System.setSecurityManager(securityManager);&lt;br /&gt;
            ConsoleApp.main(new String[] { });&lt;br /&gt;
            fail();&lt;br /&gt;
        } catch (SecurityException e) {&lt;br /&gt;
            pass();&lt;br /&gt;
            assertEquals(2, securityManager.getLastExit());&lt;br /&gt;
            assertEquals(1, securityManager.getLastExit());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
System.exit is called twice in this case, because the first System.exit throws an SecurityException which is caught in the &amp;#039;catch Throwable&amp;#039; line.  This trys to call System.exit again, and ends up throwing another exception.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    @SuppressWarnings({ &amp;quot;CallToSystemExit&amp;quot; })&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        try {&lt;br /&gt;
            if (args.length != 2) {&lt;br /&gt;
                System.err.println(&amp;quot;Usage: inputFile outputFile&amp;quot;);&lt;br /&gt;
                System.exit(1);&lt;br /&gt;
            }&lt;br /&gt;
            new ConsoleApp(args[0], args[1]).run();&lt;br /&gt;
        } catch (Throwable t) {&lt;br /&gt;
            t.printStackTrace(System.err);&lt;br /&gt;
            System.exit(2);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Brianegge</name></author>
	</entry>
</feed>