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

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:
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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.