15

When intentionally failing a test case (for example when an exception is not thrown) I've seen people use both fail() and assertTrue(false). Are there any advantages to using one or the other?

try { //method call that should throw exception fail("oops"); } catch (Exception e) {} 

vs.

try { //method call that should throw exception assertTrue("oops", false); } catch (Exception e) {} 
8
  • Why do you want to fail a test? Commented Oct 17, 2012 at 16:54
  • 4
    When an exception should have been thrown but wasn't. Do you not agree with my examples? Commented Oct 17, 2012 at 16:59
  • 1
    Exception should be an exception. A good code should never expect an exception, your code should avoid exception before it was thrown. Commented Oct 17, 2012 at 17:05
  • 5
    @Victor normally I agree but if you have a complicated Exception stack (lots of custom exceptions, possible bubbling up) you may wish to make sure the correct exception is thrown in varying cases. Commented Oct 17, 2012 at 17:09
  • 4
    @Victor I think you've been writing unit tests wrong, if you're never testing that exceptions get thrown in an error situation. You want to check failure paths as well as success paths. This will often require checking whether or not an exception has been thrown. Commented Oct 17, 2012 at 20:31

5 Answers 5

22

Are there any advantages to using one or the other?

Functionally, no. However, fail() conveys the intention more clearly, and is therefore better.

Sign up to request clarification or add additional context in comments.

Comments

13

Use JUnit's expected parameter in the @Test annotation

@Test(expected = ArithmeticException.class) public void divisionWithException() { int i = 1/0; } 

This will fail if an ArithmeticException is not thrown.

17 Comments

The OP does say "for example, when an exception is not thrown" There could be other scenarios.
I have found that the EpectedException rule is a more robust mechanism for testing expected exceptions because it can be placed immediately before the method under test thereby ensuring it was not the test setup that threw an exception.
But we are EXPECTING an exception to be thrown. So if it doesn't it should fail. Why bother with try/catch logic? We want the exception.
Concur with NullUserException
@BeRecursive Regarding your response to Izkata, I have seen a surprising number of occasions when I am expecting a NullPointerException and the test passes because it was thrown from the wrong place. Not to overkill this subject, but in my experience it is not "highly unlikely" when dealing with exceptions like NPE, IAE and Runtime.
|
5

assertTrue just calls fail.

IMHO fail() is simpler and clearer.

Comments

3

Use fail, otherwise someone will look at the failure in the log and think "What's false that should be true".

1 Comment

"What's false that should be true?" :-D
1

Both can be used (though fail is better because that's what is really intended), but since you're using JUnit, you may use @Test(expected=MyException.class) instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.