Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/ JUnit - AssertTrue vs AssertFalse

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 does assertTrue mean in Java?

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

How do you use assertTrue in JUnit?

JUnit assertTrue(). You can make use of JUnit assertTrue() in two practical scenarios. By passing condition as a boolean parameter used to assert in JUnit with the assertTrue method. It throws an AssertionError (without message) if the condition given in the method isn't True.

What is the use of assertFalse?

The assertFalse is basically a function that can be used to check if a specific logic or process will return a false statement. This can be in any conditional or structural logic that will return a boolean true or false.


assertTrue will fail if the second parameter evaluates to false (in other words, it ensures that the value is true). assertFalse does the opposite.

assertTrue("This will succeed.", true);
assertTrue("This will fail!", false);

assertFalse("This will succeed.", false);
assertFalse("This will fail!", true);

As with many other things, the best way to become familiar with these methods is to just experiment :-).


Your understanding is incorrect, in cases like these always consult the JavaDoc.

assertFalse

public static void assertFalse(java.lang.String message,
                               boolean condition)

Asserts that a condition is false. If it isn't it throws an AssertionError with the given message.

Parameters:

  • message - the identifying message for the AssertionError (null okay)
  • condition - condition to be checked

The point is semantics. In assertTrue, you are asserting that the expression is true. If it is not, then it will display the message and the assertion will fail. In assertFalse, you are asserting that an expression evaluates to false. If it is not, then the message is displayed and the assertion fails.

assertTrue (message, value == false) == assertFalse (message, value);

These are functionally the same, but if you are expecting a value to be false then use assertFalse. If you are expecting a value to be true, then use assertTrue.


I think it's just for your convenience (and the readers of your code)

Your code, and your unit tests should be ideally self documenting which this API helps with,

Think abt what is more clear to read:

AssertTrue(!(a > 3));

or

AssertFalse(a > 3);

When you open your tests after xx months when your tests suddenly fail, it would take you much less time to understand what went wrong in the second case (my opinion). If you disagree, you can always stick with AssertTrue for all cases :)


Your first reaction to these methods is quite interesting to me. I will use it in future arguments that both assertTrue and assertFalse are not the most friendly tools. If you would use

assertThat(thisOrThat, is(false));

it is much more readable, and it prints a better error message too.


assertTrue will fail if the checked value is false, and assertFalse will do the opposite: fail if the checked value is true.

Another thing, your last assertEquals will very likely fail, as it will compare the "Book was already checked out" string with the output of m1.checkOut(b1,p2). It needs a third parameter (the second value to check for equality).