assertFalse method
This lesson demonstrates how to use assertFalse method in JUnit 5 to assert test conditions.
We'll cover the following...
assertFalse() method
Assertions API provide static assertFalse()
method. This method helps us in validating that the actual value supplied to it is false
.
- If the actual value is
false
then test case will pass. - If the actual value is
true
then test case will fail.
There are basically six useful overloaded methods for assertFalse -
Press + to interact
public static void assertFalse(boolean condition)public static void assertFalse(boolean condition, String message)public static void assertFalse(boolean condition, Supplier<String> messageSupplier)public static void assertFalse(BooleanSupplier booleanSupplier)public static void assertFalse(BooleanSupplier booleanSupplier, String message)public static void assertFalse(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)

assertFalse() method
Demo
Let’s look into the usage of the above methods.
Press + to interact
package io.educative.junit5;import static org.junit.jupiter.api.Assertions.assertFalse;import org.junit.jupiter.api.Test;public class AssertFalseDemo {@Testpublic void testAssertFalseWithFalseCondition() {boolean falseValue = false;assertFalse(falseValue);}@Testpublic void testAssertFalseWithTrueCondition() {boolean trueValue = true;assertFalse(trueValue);}@Testpublic void testAssertFalseWithTrueConditionAndMessage() {boolean trueValue = true;assertFalse(trueValue, "The actual value is true");}@Testpublic void testAssertFalseWithTrueConditionAndMessageSupplier() {boolean trueValue = true;assertFalse(trueValue, () -> "The actual value is true");}@Testpublic void testAssertFalseWithBooleanSupplier() {boolean falseValue = false;assertFalse(() -> falseValue);}@Testpublic void testAssertFalseWithBooleanSupplierAndMessage() {boolean trueValue = true;assertFalse(() -> trueValue, "The actual value is true");}@Testpublic void testAssertFalseWithBooleanSupplierAndMessageSupplier() {boolean trueValue = true;assertFalse(() -> trueValue, () -> "The actual value is true");}}
You can perform code changes to above code widget, run and practice different outcomes.
Run AssertFalseDemo class as JUnit Test.
Explanation -
In AssertFalseDemo
class, there are 7 @Test
methods. These 7 methods demonstrate the working of the above 6 overloaded methods of ...