• Post Reply Bookmark Topic Watch Topic
  • New Topic

JUnit testing for System.exit  RSS feed

 
phil stephens
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a little bit of a hack in a small application of mine, a just wanted a little advice

I have a few calls to System.exit preceded by a message to screen with and explanation as to why

I wanted to unit test that a) this takes place and b) correct message is displayed

My solution was to create a unmodified subclass of RuntimeException, wrap my main method call in try/catch as so


then unit test with using org.junit.rules.ExpectedException



While this works, feels a little hacky, and was looking for advice on a possible different approach
Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Phil!

This indeed seems a bit hacky to me. You definitely shouldn't be using an exception to tell the user that they're using the application wrongly. Exceptions are for when things within your application don't work properly. That means that when the program requires main method arguments, you check them explicitly and then print a usage string to standard output.

I also don't really understand why you're explicitly calling System.exit(0). When the main method finishes running, the application will automatically finish with an exit code.

Anyway, to unit test standard output, you can replace standard output and error with print writers that write to an byte array stream. You can then check the contents of the byte array stream to see if the correct output was written.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil,
Welcome to CodeRanch!

If you do need to test System.exit(), there's a rule in this JUnit add on for it. It has a class called ExpectedSystemExit which uses a security manager to change behavior. I recommend re-writing your code to avoid the System.exit though.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phil stephens wrote:I have a few calls to System.exit preceded by a message to screen with and explanation as to why...

Well, like Stephan (and probably Jeanne too), that just strikes me as wrong.

There is almost never a good reason for calling Sysem.exit() explicitly; and if you do, you generally shouldn't be calling System.exit(0) - the reason being that 0 is what is returned when the program ends normally.

Secondly: any uncaught Exception will cause the program to terminate anyway, so calling Sysem.exit() inside an Exception seems doubly redundant.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!