I reflect on best practices for handling exceptions and unit tests because we are trying to use some of the best code methods.
In a previous article regarding best practices found in our wiki, “Don't use try / catch, but use Junit4 @Test (expect = MyException.class)”, without further information. I'm not sure.
Many of our custom exceptions have an Enum to identify the cause of the failure. As a result, I would rather see a test like:
@Test
public void testDoSomethingFailsBecauseZzz() {
try{
doSomething();
} catch(OurCustomException e){
assertEquals("Omg it failed, but not like we planned", FailureEnum.ZZZ, e.getFailure());
}
}
than:
@Test(expected = OurCustomException.class)
public void testDoSomethingFailsBecauseZzz() {
doSomething();
}
when doSomethig () looks like this:
public void doSomething throws OurCustomException {
if(Aaa) {
throw OurCustomException(FailureEnum.AAA);
}
if(Zzz) {
throw OurCustomException(FailureEnum.ZZZ);
}
}
, , @Test (expected = blabla.class) (, , , ).
- /?