JUnit Assert.assertFalse() Method Example

assertFalse() method belongs to JUnit 4 org.junit.Assert class. 
Check out JUnit 5 tutorials and examples at JUnit 5 Tutorial
In JUnit 5 all JUnit 4 assertion methods are moved to org.junit.jupiter.api.Assertions class.

When to use the assertFalse() method or assertion

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

In case we want to verify that a certain condition is true or false, we can respectively use the assertTrue() method or the assertFalse() method:
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