Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices: fail() vs assertTrue(false)

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) {}
like image 215
TrevorBliss Avatar asked Oct 17 '12 16:10

TrevorBliss


People also ask

What is the difference between assertTrue and assertFalse?

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.

What is assert assertTrue true?

assertTrue(boolean condition) Asserts that a condition is true. static void. assertTrue(java.lang.String message, boolean condition) Asserts that a condition is true.

What does assert false mean?

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. ...

What does assertTrue mean in Java?

assertTrue. public static void assertTrue(boolean condition) Asserts that a condition is true. If it isn't it throws an AssertionError without a message.


1 Answers

Are there any advantages to using one or the other?

Functionally, no. However, fail() conveys the intention more clearly, and is therefore better.

like image 128
Keppil Avatar answered Sep 29 '22 16:09

Keppil