I am writing unit tests for our main product and have one concern: how to differentiate
- tests that failed because what they tested went wrong (a bug was found and its a non regression test for it for instance)
- tests that failed because another unexpected part of the test failed (because the test is wrong or an unknown bug appeared)
For the first one, we have the jUnit Assert framework for sure, but what do we have for the second one?
Example: my unit test is testing that c() will not throw MyException, but to perform c() I need to first perform a() then b() which both can throw MyException(), so I would write:
@Test
public void testC() {
a();
Object forC = b();
try {
c(forC);
} catch (MyException e) {
Assert.fail("....");
}
}
But then I need to handle the MyException that can be thrown by a or b, and also handle the fact that forC should not be null. What is the best way to do this?
- Catch MyException thrown by a or b and Assert.fail, but a and b are not tested by this test so for me they shouldn't be marked as test failure when they fail. Maybe they fail later because at this time we should do b();a() not a();b();.
- Let testC throws MyException, so that the test will fail with "MyException", but this is misleading because MyException will not tell that the test is wrongly written. All tests will then fail each with their own Exception. In this case I would also need to throw something like NullPointerException if forC is null, which also has no semantic.
- Catch and wrap MyException thrown by a and b into a exception telling that the test might be wrong, like a TestCorruptedException. But I couldnt find such exceptions in jUnit, so they would not be recognized by jUnit as such (that's ok for me). Also I would need to know this exception from all my unit tests which are of course split into multiple modules, projects and so on...So this is doable but adds dependencies.
What would be your solution to this problem? I will likely go for the second but I'm not pleased with it as explained above.