aspose file tools*
The moose likes Testing and the fly likes How do I test System.exit(-1) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How do I test System.exit(-1)" Watch "How do I test System.exit(-1)" New topic
Author

How do I test System.exit(-1)

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Hi,

I'm using JUnit 4. I'm testing a class that throws System.exit(-1) if something goes wrong (I need this because I'm invoking the task through Ant). How do I use JUnit to detect if a System.exit(-1) has been invoked?



Thanks, - Dave


ps - Please don't post any answers saying I need to change my Java code unless your solution is also compatible with the way Ant recognizing how targets succeed and fail.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Dave,
It sounds like you are using the System.exit() to differentiate a test failure/error from some sort of horrible system failure. I don't follow how this would work though because the System.exit() would kill the JUnit test calling the code that has the horrible failure. I would say to mock out the System.exit call, but I think I am misunderstanding your scenario.

Or maybe you are testing an Ant task via JUnit. In which case, why not have the task throw a BuildException rather than System.exit?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
register the test case with SecurityManager which prevent "system.exit" by throwing SecurityException, then in the test case, you catch this exception.


in your test case:



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I test System.exit(-1)