Junit 5’s org.junit.jupiter.Assertions class provides different static assertions method to write test cases.
Assertions.assertTrue() checks if supplied boolean condition is true. In case, condition is false, it will through AssertError.
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
package org.arpit.java2blog;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class AssertTrueTest {
AssertTrueTest att;
public boolean isPrime(int number)
{
for (int i = 2; i <=Math.sqrt(number); i++) {
if(number%i==0)
return false;
}
return true;
}
@BeforeEach
public void beforeEachTest()
{
att = new AssertTrueTest();
}
//assertTrue(boolean condition)
@Test
public void primeNumberTestwithCondition(){
Assertions.assertTrue(att.isPrime(5));
}
// assertTrue(boolean condition, String message)
@Test
public void primeNumberTestwithConditionAndMessage(){
Assertions.assertTrue(att.isPrime(91),"Number is not prime");
}
// assertTrue(BooleanSupplier booleanSupplier)
@Test
public void primeNumberTestWithBooleanSupplier(){
Assertions.assertTrue(() -> att.isPrime(7));
}
// assertTrue(boolean condition, Supplier messageSupplier)
@Test
public void primeNumberTestWithConditionAndSupplier(){
Assertions.assertTrue(att.isPrime(13),() -> "Number is not prime");
}
// assertTrue(BooleanSupplier booleanSupplier, String message)
@Test
public void primeNumberTestWithBooleanSupplierAndMessage(){
Assertions.assertTrue(() -> att.isPrime(17),"Number is not prime");
}
// assertTrue(BooleanSupplier booleanSupplier, Supplier messageSupplier)
@Test
public void primeNumberTestWithBooleanSupplierAndSupplier(){
Assertions.assertTrue(() -> att.isPrime(89),() -> "Number is not prime");
}
}
When you run above testcase, you will get below output:

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.