JUnit assertFalse 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 assertFalse method.

Assertions.AssertFalse() checks if supplied boolean condition is false. In case, condition is true, it will through AssertError.

public static void assertFalse(boolean condition)
public static void assertFalse(boolean condition, Supplier messageSupplier)
public static void assertFalse(BooleanSupplier booleanSupplier)
public static void assertFalse(BooleanSupplier booleanSupplier, String message)
public static void assertFalse(boolean condition,String message)
public static void assertFalse(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 AssertFalseTest {

	AssertFalseTest att;

    public boolean isPalindrome(String str)
	{
		for (int i = 0,j=str.length()-1; i <str.length()/2; i++,j--) {

			if(str.charAt(i)!=str.charAt(j))
			{
				return false;
			}

		}
		return true;
	}

	@BeforeEach
	public void beforeEachTest()
	{
		att = new AssertFalseTest();
	}

	//assertFalse(boolean condition)
	@Test
	public void palindormeTestwithCondition(){
		Assertions.assertFalse(att.isPalindrome("ADAM"));
	}

	// assertFalse(boolean condition, String message)
	@Test
	public void palindormeTestwithConditionAndMessage(){
		Assertions.assertFalse(att.isPalindrome("MADAM"),"String is palindrome");
	}

	// assertFalse(BooleanSupplier booleanSupplier)
	@Test
	public void palindormeTestWithBooleanSupplier(){
		Assertions.assertFalse(() -> att.isPalindrome("ASDAS"));
	}

	// 	assertFalse(boolean condition, Supplier<String> messageSupplier)
	@Test
	public void palindormeTestWithConditionAndSupplier(){
		Assertions.assertFalse(att.isPalindrome("LEVEL"),() -> "String is palindrome");
	}

	// assertFalse(BooleanSupplier booleanSupplier, String message)
	@Test
	public void palindormeTestWithBooleanSupplierAndMessage(){
		Assertions.assertFalse(() -> att.isPalindrome("PLAN"),"String is palindrome");
	}

	// assertFalse(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)
	@Test
	public void palindormeTestWithBooleanSupplierAndSupplier(){
		Assertions.assertFalse(() -> att.isPalindrome("RADAR"),() -> "String is palindrome");
	}
}

When you run above testcase, you will get below output:
AssertFalseTestOutput

Let’s understand output of each testcase:


palindormeTestwithCondition – Pass

As “ADAM” is not palindrome, so this testcase will pass.


palindormeTestwithConditionAndMessage – Fail

As “MADAM” is palindrome, so this testcase will fail.


palindormeTestWithBooleanSupplier – Pass

As “ASDAS” is not palindrome, so this testcase will pass.


palindormeTestWithConditionAndSupplier – Fail

As “LEVEL” is palindrome, so this testcase will fail.


palindormeTestWithBooleanSupplierAndMessage – Pass

As “PLAN” is not palindrome, so this testcase will pass.


palindormeTestWithBooleanSupplierAndSupplier – Fail

As “RADAR” is palindrome, so this testcase will fail.


That’s all about Assertions.assertFalse()

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