JUnit Assert.assertTrue() Method Example

assertTrue() method belongs to JUnit 4 org.junit.Assert class. In JUnit 5 all JUnit 4 assertion methods are moved to org.junit.jupiter.api.Assertions class.

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

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

Assert.assertTrue() Method Example

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;
 }
 
 
 @Test
 public void isEmptyTest() {
     assertTrue(isEmpty(null));
     assertTrue(isEmpty(""));
     assertFalse(isEmpty(" "));
     assertFalse(isEmpty("bob"));
     assertFalse(isEmpty("  bob  "));
 }
 
 @Test
 public void isBlankTest() {
     assertTrue(isBlank(null));
     assertTrue(isBlank(""));
     assertTrue(isBlank(" "));
     assertFalse(isBlank("bob"));
     assertFalse(isBlank("  bob  "));
  }
}

Output:

Related JUnit Examples


Comments