Reputation: 2562
Is assert(false);
an acceptable way to cause a test to fail?
Also, should I even be causing a TestNG test to fail if a NoSuchMethodException or SecurityException is thrown while trying to access private methods of a class or should I just print the exception's stack trace?
@Test
public void getLevelTest() {
try {
menu.getClass().getDeclaredMethod("getLevel",int.class,String.class);
} catch (NoSuchMethodException | SecurityException e) {
assert(false);
}
}
Upvotes: 2
Views: 8445
Reputation: 812
Another way to handle expected exceptions in the TestNG framework is:
@Test(expectedExceptions = {NoSuchMethodException.class, SecurityException.class})
If the test annotated with this encounters any of the exceptions, it will be considered a pass.
Upvotes: 3
Reputation: 96444
In this case using assert(false)
means you lose the information about what exception was thrown. TestNG doesn't know that the exception had anything to do with the test failure.
You shouldn't need to assert anything or catch anything:
@Test
public void getLevelTest() throws Exception {
menu.getClass().getDeclaredMethod("getLevel",int.class,String.class);
}
The test framework will capture the exception and print the stack trace in the test results.
Upvotes: 2
Reputation:
Use assertTrue()
and assertFalse()
, also see old answer on this topis.
Upvotes: 2