• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit testing for System.exit

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Can we write test to various (customm) System.exit in code.?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Didn't this topic give you any answers?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I had replied a few minutes ago.

Yes, you can, but it is better to avoid System.exit altogether.

And . . . welcome to the Ranch (again).
 
Aishu hsrad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having ExitCodeExceptionMapper in main class. that will exit app with different exitcode according to the exception caught(exception from all files). stuck at writing test for different cases and it's exit code(1/2/3).?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Priya darsh wrote:. . . exit app with different exitcode according to the exception caught . . .

Sorry.
That sounds strange and probably incorrect to me.
 
Aishu hsrad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




How can i write test to cover the above lines inside exitCodeExceptionMapper..?  i will catch exception and based on the type of exception i'll show the exitcode..
reply
    Bookmark Topic Watch Topic
  • New Topic