Search⌘ K
AI Features

assertFalse method

Explore the assertFalse method in JUnit 5 assertions to validate that a condition is false. Understand its six overloaded versions, including usage with boolean values, boolean suppliers, and custom messages, to write reliable unit tests that pass or fail based on the actual condition.

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 -

Java
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)
Video thumbnail
assertFalse() method

Demo

Let’s look into the usage of the above methods.

JUnit5 (Java 1.8)
package io.educative.junit5;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
public class AssertFalseDemo {
@Test
public void testAssertFalseWithFalseCondition() {
boolean falseValue = false;
assertFalse(falseValue);
}
@Test
public void testAssertFalseWithTrueCondition() {
boolean trueValue = true;
assertFalse(trueValue);
}
@Test
public void testAssertFalseWithTrueConditionAndMessage() {
boolean trueValue = true;
assertFalse(trueValue, "The actual value is true");
}
@Test
public void testAssertFalseWithTrueConditionAndMessageSupplier() {
boolean trueValue = true;
assertFalse(trueValue, () -> "The actual value is true");
}
@Test
public void testAssertFalseWithBooleanSupplier() {
boolean falseValue = false;
assertFalse(() -> falseValue);
}
@Test
public void testAssertFalseWithBooleanSupplierAndMessage() {
boolean trueValue = true;
assertFalse(() -> trueValue, "The actual value is true");
}
@Test
public 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.

widget

Explanation -

In AssertFalseDemo class, there are 7 @Test methods. These 7 methods demonstrate the working of the above 6 overloaded methods of ...