JUnit assertTrue example

Junit 5’s org.junit.jupiter.Assertions class provides different static assertions method to write test cases.

Please note that you need to use JUnit’s org.junit.Assert class in case of JUnit 4 or JUnit 3 to assert using assertTrue method.

Assertions.assertTrue() checks if supplied boolean condition is true. In case, condition is false, it will through AssertError.

public static void assertTrue(boolean condition)
public static void assertTrue(boolean condition, Supplier messageSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier, String message)
public static void assertTrue(boolean condition,String message)
public static void assertTrue(BooleanSupplier booleanSupplier,Supplier messageSupplier)

Here is simple example

When you run above testcase, you will get below output:
AssertTrueTestOutput

Let’s understand output of each testcase:


primeNumberTestwithCondition – Pass

As 5 is prime number, assertTrue will return true and this testcase will pass.


primeNumberTestwithConditionAndMessage – Fail

As 91 is not prime number, assertTrue will return true and this testcase will fail.


primeNumberTestWithBooleanSupplier – Pass

As 13 is prime number, assertTrue will return true and this testcase will pass.


primeNumberTestWithConditionAndSupplier – Pass

As 5 is prime number, assertTrue will return true and this testcase will pass.


primeNumberTestWithBooleanSupplierAndMessage – Pass

As 17 is prime number, assertTrue will return true and this testcase will pass.


primeNumberTestWithBooleanSupplierAndSupplier – Pass

As 89 is prime number, assertTrue will return true and this testcase will pass.


That’s all about JUnit assertTrue example.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *