JUnit Assert.assertTrue() Method Example

In this post, we will learn how to use the assertTrue() method with an example. 

The assertTrue() method belongs to JUnit 4 org.junit.Assert class.
Check out JUnit 5 tutorials and examples at https://www.javaguides.net/p/junit-5.html
In JUnit 5all JUnit 4 assertion methods are moved to org.junit.jupiter.api.Assertions class.

When to use assertTrue() method

In case we want to verify that a certain condition is true or false, we can respectively use the assertTrue assertion or the assertFalse one.

void org.junit.Assert.assertTrue(boolean condition)

Asserts that a condition is true. If it isn't it throws an AssertionError without a message. 
  • condition - condition to be checked

Assert.assertTrue() method example

The below example demonstrates how to use assertTrue() method check empty or null condition methods:
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class AssertTrueExample {

 public static boolean isEmpty(final CharSequence cs) {
     return cs == null || cs.length() == 0;
 public static boolean isBlank(final CharSequence cs) {
     int strLen;
     if (cs == null || (strLen = cs.length()) == 0) {
         return true;
     for (int i = 0; i < strLen; i++) {
         if (!Character.isWhitespace(cs.charAt(i))) {
             return false;
     return true;
 public void isEmptyTest() {
     assertFalse(isEmpty(" "));
     assertFalse(isEmpty("  bob  "));
 public void isBlankTest() {
     assertTrue(isBlank(" "));
     assertFalse(isBlank("  bob  "));


Related JUnit Examples