• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I test System.exit(-1)

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic