JUnit Assert.assertFalse() Method Example

assertFalse() 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.assertFalse(boolean condition)

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

Assert.assertFalse(boolean condition) Method Example

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class AssertFalseExample {
 /**
  * <p>
  * Check whether the given CharSequence contains any whitespace characters.
  * </p>
  *
  * <p>
  * Whitespace is defined by {@link Character#isWhitespace(char)}.
  * </p>
  *
  * @param seq
  *            the CharSequence to check (may be {@code null})
  * @return {@code true} if the CharSequence is not empty and contains at
  *         least 1 (breaking) whitespace character
  * @since 3.0
  */
 public static boolean containsWhitespace(final CharSequence seq) {
      if (seq == null || seq.length() == 0) {
          return false;
      }
      final int strLen = seq.length();
      for (int i = 0; i < strLen; i++) {
          if (Character.isWhitespace(seq.charAt(i))) {
              return true;
          }
      }
      return false;
  }
 
 @Test
 public void testContainsWhitespace() {
      assertFalse(containsWhitespace(null));
      assertFalse(containsWhitespace(""));
      assertFalse(containsWhitespace("a"));
      assertFalse(containsWhitespace("abc"));
      assertTrue(containsWhitespace(" "));
      assertTrue(containsWhitespace(" a"));
      assertTrue(containsWhitespace("abc "));
      assertTrue(containsWhitespace("a b"));
      assertTrue(containsWhitespace("a  b"));
   }
}

Output:

Related JUnit Examples

Comments