What's the actual use of 'fail' in JUnit test case?

后端 未结 8 1146
长情又很酷
长情又很酷 2021-01-30 15:31

What\'s the actual use of \'fail\' in JUnit test case?

8条回答
  •  庸人自扰
    2021-01-30 16:12

    I think the usual use case is to call it when no exception was thrown in a negative test.

    Something like the following pseudo-code:

    test_addNilThrowsNullPointerException()
    {
        try {
            foo.add(NIL);                      // we expect a NullPointerException here
            fail("No NullPointerException");   // cause the test to fail if we reach this            
         } catch (NullNullPointerException e) {
            // OK got the expected exception
        }
    }
    

提交回复
热议问题