Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to handle the exception for JUnit code

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose in my JUNIT code I have

public void testMethod() { // shall I just throw Exception
try {
String s = method();
assertTrue(s, null);
...
} catch(Exception e) {
// ??? what to do with Exception e ? // line 6
}
}


Suppose this junit method tests a method called "Method()" which returns a String value and this method throws "Exception" out. Then in my "testMethod()" code, I am wondering

a) if I just throw it out again, it does not sounds like a good way for testing

b) how about I catch it in "testMethod()" (line 6) ? If I catch it, shall I just ignore it ? Or shall I print out the information ? Or shall I write the error to a log file ?

What's the typical way to do it when we are using junit ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you'd better declare the exception in the throws clause of the test method.

 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) if I just throw it out again, it does not sounds like a good way for testing.


Basically, your questioning is correct - you just have to decide on the basis of what your code needs to accomplish.

b) how about I catch it in "testMethod()" (line 6) ? If I catch it, shall I just ignore it ? Or shall I print out the information ? Or shall I write the error to a log file ?


Generally, write to a log file. Do not ignore exceptions without great thought as to what that will do. Occasionally that is the way do do it but maybe unchecked exception would be better for that harness.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception handling in JUnit tests is a bit different to in a typical application.

If an exception can happen as part of correct operation of the methods being tested, then you need to catch and handle the exception in your test method.



If the exception should never happen as part of correct operation, then your test method should throw it out to JUnit infrastructure. The JUnit infrastructure will interpret this as a test failure. There is no need to catch such exceptions in your test method and do fail(); in fact, this throws away useful information that JUnit could have used in its test report.



There is another situation. That is where you have a JUnit test whose purpose is to check that the method being tested does throw the exception it should throw, when given particular (erroneous) inputs. In that situation, you do something like this: -



Remember that it is at least as important for your JUnit tests to test unusual and exceptional cases as it is to test the normal case. Many people would say it is more important, because a typical "smoke test" will exercise the normal case, but only rigorous testing will exercise unusual cases.
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends...

Normally if I'm testing something that throws a checked exception I'll explicitly put a fail() in the catch block when I'm testing the happy path. When I'm testing the exception path, I'll put the fail in the try block after the line that should throw the exception and an explicit pass()/assertTrue(true) in the catch block.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic