aspose file tools*
The moose likes Testing and the fly likes How to test the void method having System.exit(-1) in Junit 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 to test the void method having System.exit(-1) in Junit" Watch "How to test the void method having System.exit(-1) in Junit" New topic
Author

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

Deepa Rajesh
Greenhorn

Joined: Nov 15, 2012
Posts: 6
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)
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

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



  • Junilu Lacar
    Bartender

    Joined: Feb 26, 2001
    Posts: 5274
        
      10

    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.



    Junilu - [How to Ask Questions] [How to Answer Questions]
    Greg Charles
    Sheriff

    Joined: Oct 01, 2001
    Posts: 2864
        
      11

    Very nice! That is a better way to do it.
    Jeanne Boyarsky
    author & internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 31057
        
    232

    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.


    [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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to test the void method having System.exit(-1) in Junit