I am trying to do a JUnit test on code that someone else has written, but I cannot figure out how to test for the exception, because the exception seems to lack a type.
You could either use expected in @Test annotation or provide an explicit catch block and issue a fail if the program flow is not as expected.
@Test(expected=Exception.class) // java.lang.Exception
public static void exceptionTest() throws Exception {
rodgers = new Pirate("Dread Pirate Rodgers" , -100);
}
@Test
public static void exceptionTest() throws Exception {
try {
rodgers = new Pirate("Dread Pirate Rodgers" , -100);
fail("should not reach this");
} catch(Exception e) {
// ok
}
}
My personal preference is the first solution.