Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why does it call <code>System.exit()</code>? And why does it call <code>System.exit()</code> at a level you're covering with your unit test?</p> <p>If you want to test the exiting code, your tests can spawn a process and check that it has exited as planned. If you want to test other functionality, better structure your application in such a way that the code under test doesn not have such nasty side effects.</p> <p>In general though, I'd say that having individual threads call <code>System.exit()</code> is a strong indicator of bad architecture. If any thread can instantly kill the entire appication without giving other threads a chance to clean up you'll likely run into quite some problems quite soon.</p> <p>And if your threads call some global shutdown handler (that you can override in your tests) instead of <code>System.exit()</code> you won't have a problem testing that functionality.</p> <p><strong>Update:</strong> When dealing with legacy code, my first step would be to isolate the <code>System.exit()</code> into a place where it can be replaced in tests. E.g. create a class like this:</p> <pre><code>// "Singleton" with replaceable instance public class ShutdownHandler { private static ShutdownHandler instance = new ShutdownHandler(); public static ShutdownHandler getInstance() { return instance; } public synchronized void shutdown() { // default implementation System.exit(); } public static void setInstance(ShutdownHandler newInstance) { // (probably also check that this is only called in a test environment) instance = newInstance; } } </code></pre> <p>Then replace all <code>System.exit()</code> calls with <code>ShutdownHandler.getInstance().shutdown()</code>. That won't change the application's functionality at all. <em>But</em> now you can replace the <code>ShutdownHandler</code> instance in your tests with an implementation that sets some flag + throws an exception instead of exiting -- and your tests can then check that flag to determine whether the app would have exited.</p> <p>I can also recommend "Working Effectively With Legacy Code" by Robert C. Martin for more ideas on how to turn an old mess into something manageable.</p>
    singulars
    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. 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