...

/

Assertions: assertTrue() and assertFalse()

Assertions: assertTrue() and assertFalse()

Learn about the assertTrue() and assertFalse() methods in JUnit 5.

What are assertions?

Assertions verify that expected conditions are met. JUnit 5 adds more assertion methods. All JUnit 5 assertions are static methods of the class org.junit.jupiter.Assertions. Some assertions already exist in JUnit 4. These assertions have been updated in JUnit 5. Each assertion has different overloaded methods to accept different parameters.

The assertTrue() method

The assertTrue() method asserts that the given condition is true.

  • If the given condition is true, the test case passes.
  • If the given condition is false, the test case fails.

Examples of assertTrue()

It has the following overloaded methods:

Press + to interact
public static void assertTrue(boolean condition)
public static void assertTrue(boolean condition, String message)
public static void assertTrue(boolean condition, Supplier<String> messageSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier, String message)
public static void assertTrue(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)
  • The assertTrue(boolean condition) method is the simplest form, which only accepts a single boolean parameter. Examples of this function can be seen in lines 5–9 and 11–15 in the code below. Note that the function testAssertTrueWithTrueCondition() passes because it’s passed a true value, whereas the function testAssertTrueWithFalseCondition() doesn’t pass because false is passed.

  • For the assertTrue(boolean condition, String message) method, when the condition isn’t ...