Methods in JUnit Assertions class
We have seen what JUnit is and what the annotations used in the JUnit are. Now we will see JUnit Assertions class methods used in writing the test cases.
All the assertions are present in this class.
We will see the class methods and examples. One for each method name. In details for each method overloaded type you can refer to JUnit-5
- static void assertAll(Executable… executables)
This is used to assert that all executables passed inside the input do not throw any exceptions.
In a grouped assertion all assertions are executed, and any failures will be reported together.
Example: If we change any expected value to the wrong addition number then we will able to see the fail case. Note that all the cases are executed and reported together irrespective of the position/count of failed cases.
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
Add add = new Add();
assertAll(() -> assertEquals(25, add.addNumber(20, 5))
,() -> assertEquals(20, add.addNumber(17, 3))
,() -> assertEquals(14, add.addNumber(7, 7))
,() -> assertEquals(85, add.addNumber(37, 48))
);
}
}
class Add {
public int addNumber(int a, int b) {
return a + b;
}
}
- static void assertArrayEquals(Object[] expected, Object[] actual)
This will assert that if expected and actual int arrays are equal.
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
Generate generate = new Generate();
Integer[] expectedValues = new Integer[] {20, 5};
assertArrayEquals(expectedValues, generate.getArray(20, 5));
}
}
class Generate {
public Integer[] getArray(int a, int b) {
return new Integer[] {a, b};
}
}
- static void assertEquals(Object expected, Object actual)
This will assert if the expected and actual values are equal.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
Add add = new Add();
assertEquals(25, add.addNumber(20, 5));
}
}
class Add {
public int addNumber(int a, int b) {
return a + b;
}
}
- static void assertFalse(boolean condition)
Asserts that the supplied condition is not true.
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
Check check = new Check();
assertFalse(check.isEven(25));
// This will fail with exception printing the message provided.
// org.opentest4j.AssertionFailedError: Number is not even ==> expected: <false> but was: <true>
assertFalse(check.isEven(20), “Number is not even”);
}
}
class Check{
public boolean isEven(int a) {
return a % 2 == 0;
}
}
- static void assertIterableEquals(Iterable<?> expected, Iterable<?> actual)
This will assert it expected and actual iterable values are deeply equal.
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
List<Integer> actual = new ArrayList<Integer>(Arrays.asList(1,5,3,2));
List<Integer> expected = new ArrayList<Integer>(Arrays.asList(1,5,3,2));
assertIterableEquals(expected, actual);
}
}
- static void assertLinesMatch(List<String> expectedLines, List<String> actualLines)
This will assert if the expected list of Strings matches the actual list.
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
List<String> actual = new ArrayList<String>(Arrays.asList(“ABCD”, “LMNO”, “pqrs”));
List<String> expected = new ArrayList<String>(Arrays.asList(“ABCD”, “LMNO”, “pqrs”));
assertLinesMatch(expected, actual);
}
}
- static void assertNotEquals(Object expected, Object actual)
This will assert if the expected and actual values are not equal.
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
int expected = 26;
assertNotEquals(expected, (23+9));
}
}
- static void assertNotNull(Object actual)
This will assert if the actual value is not null.
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
List<Integer> l1 = new ArrayList<>();
assertNotNull(l1);
}
}
- static void assertNotSame(Object expected, Object actual)
This will assert if expected and actual values refer to the same object. Since we have created two different objects using a new String, these two objects will have different addresses. If we create the string object using String expected = “ABCD” and String actual = “ABCD” our test case will fail since both will be pointing to the same address because of the immutable nature of the String class
import static org.junit.Assert.assertNotSame;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
String expected = new String(“ABCD”);
String actual = new String(“ABCD”);
assertNotSame(expected, actual);
}
}
- static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable)
This will assert if the execution of the supplied executable throws an exception of the expectedType and returns the exception.
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
String a = null;
assertThrows(NullPointerException.class, () -> {a.charAt(0);});
}
}
- static void assertTimeout(Duration timeout, Executable executable)
This will assert if the execution of the supplied executable completes before the given timeout is exceeded. In the below example, we can decrease/increase the timeout and check the response.
import static org.junit.jupiter.api.Assertions.assertTimeout;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
AtomicInteger i = new AtomicInteger();
assertTimeout(Duration.ofMillis(10000), () -> {
while(i.get()< 1000) {
i.set(i.get()+1);
System.out.println(“test”);
}
});
}
}
- static void assertTimeoutPreemptively(Duration timeout, Executable executable)
This will assert if the execution of the supplied executable completes before the given timeout is exceeded.
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
AtomicInteger i = new AtomicInteger();
assertTimeoutPreemptively(Duration.ofMillis(1), () -> {
while(i.get()< 1000) {
i.set(i.get()+1);
System.out.println(“test”);
}
});
}
}
- static void assertTrue(boolean condition)
This will assert if the supplied condition is true. We will check if the given integer is an even number or not
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
assertTrue(40 % 2 == 0);
}
}
- static <V> V fail(String message)
This will Fails a test with the given failure message. In the below example, we will purposely fail the test case if a number is odd.
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.Test;
public class TestClass {
@Test
public void test() {
int i = 21;
// failing the case if number is odd
if (i % 2 != 0) {
fail(“Number is ODD”);
}
}
}