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

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

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

8条回答
  •  醉梦人生
    2021-01-30 16:07

    The most important use case is probably exception checking.

    While junit4 includes the expected element for checking if an exception occurred, it seems like it isn't part of the newer junit5. Another advantage of using fail() over the expected is that you can combine it with finally allowing test-case cleanup.

    dao.insert(obj);
    try {
      dao.insert(obj);
      fail("No DuplicateKeyException thrown.");
    } catch (DuplicateKeyException e) {
      assertEquals("Error code doesn't match", 123, e.getErrorCode());
    } finally {
      //cleanup
      dao.delete(obj);
    }
    

    As noted in another comment. Having a test to fail until you can finish implementing it sounds reasonable as well.

提交回复
热议问题