What's the difference between failure and error in JUnit?

I'm running JUnit tests on a large code base, and I've been realizing that sometimes I get "Errors" while other times I get "Failures". What's the difference?


It seems that failures are when your test cases fail – i.e. your assertions are incorrect. Errors are unexpected errors that occur while trying to actually run the test – exceptions, etc.


If your test throws an exception which does not get bubbled up through the Assertion framework in Junit, it gets reported as an error. For example, a NullPointer, or a ClassNotFound exception will report an error:

String s = null;
s.trim();

or,

try {

    // your code
} catch(Exception e) {
    // log the exception
    throw new MyException(e);
}

Having said that, the following will report a failure:

Assert.fail("Failure here");

or,

Assert.assertEquals(1, 2);

or even:

throw new AssertionException(e);

It depends on the Junit version you are using. Junit 4- will make the distinction between a failure and an error, but Junit 4 simplifies it as failures only.

Following link provides more interesting inputs:

http://www.devx.com/Java/Article/31983/1763/page/2