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

后端 未结 8 2483
粉色の甜心
粉色の甜心 2020-11-30 06:27

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?

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 06:48

    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

提交回复
热议问题