Table of Contents
Junit 5’s org.junit.jupiter.Assertions class provides different static assertions method to write test cases.
Assertions.AssertFalse() checks if supplied boolean condition is false. In case, condition is true, it will through AssertError.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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:
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()