What\'s the actual use of \'fail\' in JUnit test case?
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.