• 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

How to test the void method having System.exit(-1) in Junit

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Standalone program which has a void method and will exit if any validation fails.If i give the input which will fail in validation , Test case has stopped and it is not continuing to the next case.


Please help to write the proper test case for this situation.( I am very new to JUNIT)
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python 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
Some sort of mocking framework might help, but here's something that would work with pure JUnit:

  • In your class, defne a protected exit() method that just calls System.exit(-1)
  • Change all other calls to System.exit(-1) to use exit() instead.
  • In your JUnit test, override your class, and redefine exit() to throw a runtime exception instead of calling System.exit(-1)
  • Now in your tests you can catch the exception and put assertions in the catch block depending on whether you expected an exit or not



  •  
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have used an approach similar to what Greg suggested. Extending the class under test (CUT) and overriding one or more methods works really nicely, assuming the CUT is not declared as final. However, throwing an exception is expensive and increases test execution time slightly. Since unit tests should run as fast as possible, I'd prefer to avoid throwing an exception outside of the actual CUT if I can help it. I would just add a boolean field in the extended class and set it to true in the redefined exit() method. No need to add a getter, just access the boolean field directly from the test case.

     
    Greg Charles
    Sheriff
    Posts: 3063
    12
    Mac IntelliJ IDE Python VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Very nice! That is a better way to do it.
     
    author & internet detective
    Posts: 41860
    908
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As an alternative to the approaches described above, you could write a mockable helper class:



    This gives you a nice class with an interface that you can mock out normally.
    reply
      Bookmark Topic Watch Topic
    • New Topic