8

I'm learning to use JUnit.

Some of my tests have come up as "errors" and some as "failures". What's the difference between the two?

rajesh
  • 3,247
  • 5
  • 31
  • 56
papercuts
  • 1,428
  • 4
  • 15
  • 16

4 Answers4

13

An error is when something breaks and an exception occurs, such as a Null object reference.

A failure is when the test criteria is not met. i.e. when the Assert() fails.

[This is in general, not just junit.]

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
4

In simple words,

Errors - mean that while your test was running, there were some unhandled/unforeseen exceptions, and hence, your test case basically crashed without executing fully.

Failures - mean that your test completed successfully, but the test condition of your test criteria has failed(not what you expected it to be).

Rahul
  • 44,383
  • 11
  • 84
  • 103
2

A failure is when one of your assertions fails--that is, your program does something wrong, and your JUnit test notices and reports the fact.

An error is when some other Exception occurs--one you haven't tested for and didn't expect, such as a NullPointerException or an ArrayIndexOutOfBoundsException.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
2

Failure - When test case fails (Condition that you assert did not succeed)

Error - Unexpected scenarios or errors in executing the test case

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120