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

Posted by Chris Conway on Stack Overflow See other posts from Stack Overflow or by Chris Conway
Published on 2008-11-21T16:38:58Z Indexed on 2010/04/10 17:23 UTC
Read the original article Hit count: 246

I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just the current thread. Are there any common patterns for dealing with this? For example, can I subsitute a stub for System.exit()?

[EDIT] The class in question is actually a command-line tool which I'm attempting to test inside JUnit. Maybe JUnit is simply not the right tool for the job? Suggestions for complementary regression testing tools are welcome (preferably something that integrates well with JUnit and EclEmma).

© Stack Overflow or respective owner

Related posts about java

Related posts about junit