Table of Contents
Junit 5’s org.junit.jupiter.Assertions class provides different static assertions method to write test cases.
Assertions.assertTrue() checks if supplied boolean condition is true. In case, condition is false, it will through AssertError.
public static void assertTrue(boolean condition, Supplier messageSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier, String message)
public static void assertTrue(boolean condition,String message)
public static void assertTrue(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 |
package org.arpit.java2blog; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class AssertTrueTest { AssertTrueTest att; public boolean isPrime(int number) { for (int i = 2; i <=Math.sqrt(number); i++) { if(number%i==0) return false; } return true; } @BeforeEach public void beforeEachTest() { att = new AssertTrueTest(); } //assertTrue(boolean condition) @Test public void primeNumberTestwithCondition(){ Assertions.assertTrue(att.isPrime(5)); } // assertTrue(boolean condition, String message) @Test public void primeNumberTestwithConditionAndMessage(){ Assertions.assertTrue(att.isPrime(91),"Number is not prime"); } // assertTrue(BooleanSupplier booleanSupplier) @Test public void primeNumberTestWithBooleanSupplier(){ Assertions.assertTrue(() -> att.isPrime(7)); } // assertTrue(boolean condition, Supplier<String> messageSupplier) @Test public void primeNumberTestWithConditionAndSupplier(){ Assertions.assertTrue(att.isPrime(13),() -> "Number is not prime"); } // assertTrue(BooleanSupplier booleanSupplier, String message) @Test public void primeNumberTestWithBooleanSupplierAndMessage(){ Assertions.assertTrue(() -> att.isPrime(17),"Number is not prime"); } // assertTrue(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier) @Test public void primeNumberTestWithBooleanSupplierAndSupplier(){ Assertions.assertTrue(() -> att.isPrime(89),() -> "Number is not prime"); } } |
When you run above testcase, you will get below output:
Let’s understand output of each testcase:
primeNumberTestwithCondition – Pass
As 5 is prime number, assertTrue will return true and this testcase will pass.
primeNumberTestwithConditionAndMessage – Fail
As 91 is not prime number, assertTrue will return true and this testcase will fail.
primeNumberTestWithBooleanSupplier – Pass
As 13 is prime number, assertTrue will return true and this testcase will pass.
primeNumberTestWithConditionAndSupplier – Pass
As 5 is prime number, assertTrue will return true and this testcase will pass.
primeNumberTestWithBooleanSupplierAndMessage – Pass
As 17 is prime number, assertTrue will return true and this testcase will pass.
primeNumberTestWithBooleanSupplierAndSupplier – Pass
As 89 is prime number, assertTrue will return true and this testcase will pass.
That’s all about JUnit assertTrue example.