When intentionally failing a test case (for example when an exception is not thrown) I've seen people use both fail() and assertTrue(false). Are there any advantages to using one or the other?
try {
//method call that should throw exception
fail("oops");
} catch (Exception e) {}
vs.
try {
//method call that should throw exception
assertTrue("oops", false);
} catch (Exception e) {}
AssertTrue method asserts that a specified condition is true. It throws an AssertionError if the condition passed to the asserttrue method is not satisfied. AssertFalse method asserts that a specified condition is false. It throws an AssertionError if the condition passed to assert false method is not satisfied.
assertTrue(boolean condition) Asserts that a condition is true. static void. assertTrue(java.lang.String message, boolean condition) Asserts that a condition is true.
In assert True, you are asserting that the expression is true. If it is not, then it will display the message and the assertion will fail. In assert False, you are asserting that an expression evaluates to false. ...
assertTrue. public static void assertTrue(boolean condition) Asserts that a condition is true. If it isn't it throws an AssertionError without a message.
Are there any advantages to using one or the other?
Functionally, no. However, fail()
conveys the intention more clearly, and is therefore better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With