Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>System.exit(status)</code> actually delegates the call to <em>Runtime</em> class. <em>Runtime</em> before proceeding with this shutdown request invokes <code>checkExit(status)</code> on JVM's current <em>SecurityManager</em> which can prevent the impending shutdown by throwing a <em>SecurityException</em>.</p> <p>Usually, the <em>SecurityManager</em> needs to establish if the current thread has the privilege to shutdown defined by the current security policy in place but since all we need is to recover from this exit call we simply throw a <em>SecurityException</em> that we'll now have to catch in our JUnit test case.</p> <p>In your JUnit test class, setup a <em>SecurityManager</em> in <code>setUP()</code> method:</p> <pre><code> securityManager = System.getSecurityManager(); System.setSecurityManager(new SecurityManager() { @Override public void checkExit(int status) { super.checkExit(status); // This is IMPORTANT! throw new SecurityException("Overriding shutdown..."); } }); </code></pre> <p>In <code>tearDown()</code> replace the <em>SecurityManager</em> again with the instance that we saved before. Failure to do so would prevent JUnit from shutting down now! :)</p> <p>References:<br /> <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SecurityManager.html" rel="nofollow">http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SecurityManager.html</a><br /> <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SecurityManager.html#checkExit(int)" rel="nofollow">http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SecurityManager.html#checkExit(int)</a></p> <blockquote> <p>The SecurityManager class contains many methods with names that begin with the word check. These methods are called by various methods in the Java libraries before those methods perform certain potentially sensitive operations. The invocation of such a check method typically looks like this:</p> </blockquote> <pre><code> SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkXXX(argument, . . . ); } </code></pre> <blockquote> <p>The security manager is thereby given an opportunity to prevent completion of the operation by throwing an exception. A security manager routine simply returns if the operation is permitted, but throws a SecurityException if the operation is not permitted. </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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