JUnit provides Assert all to assert all the scenario of a test case. assertAll() is introduced in JUnit 5. It checks all the assertions and reports the individual results of failure or success.
Checkout Junit Tutorial here.
What is assertall in Junit 5 ?
One of the new feature of JUnit 5 assertAll(), it allows the creation of grouped assertions, where all the assertions are executed, and their failures are reported together.
Javadoc of Assertions.assertAll() states that:
Asserts that all supplied executables do not throw exceptions.
-javaDoc
assertAll() groups all assertions and errors that were passed to that invocation of assertAll().
assertAll in JUnit 5 Example:
Let’s take an example of assert all, where in we will check all the assertions and will look for failures.
First, we need to add Junit 5 to the class path via Jar or Maven dependency.
package com.slb.thread;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AssertAllTest {
@Test
public void assertAllTestCase(){
//we will be testing addition use case for multiple assertions
assertAll(
() -> {assertEquals(4, addition(2,2));},
() -> {assertEquals(7, addition(5,2));},
() -> {assertEquals(5, addition(2,3));},
() -> {assertEquals(5, addition(3,2));},
() -> {assertEquals(55, addition(52,3));}
);
}
private int addition(int i, int j) {
System.out.println("adding "+ i + "with:" + j);
return i + j;
}
}
In the above example, we are using one addition function, and we are performing multiple assertions for these. Assert all takes executable, so we have used lambda expression for multiple assertions. The above case works fine as there is no failure scenario, hence you can see below output:
adding 2with:2
adding 5with:2
adding 2with:3
adding 3with:2
adding 52with:3
Process finished with exit code 0
Now, let’s make a failure case by changing the expectation of () -> {assertEquals(5, addition(3,2));} this to wrong one.
package com.slb.thread;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AssertAllTest {
@Test
public void assertAllTestCase(){
//we will be testing addition use case for multiple assertions
assertAll(
() -> {assertEquals(4, addition(2,2));},
() -> {assertEquals(7, addition(5,2));},
() -> {assertEquals(5, addition(2,3));},
() -> {assertEquals(25, addition(3,2));}, //wrong one
() -> {assertEquals(55, addition(52,3));}
);
}
private int addition(int i, int j) {
System.out.println("adding "+ i + "with:" + j);
return i + j;
}
}
Output:
adding 2with:2
adding 5with:2
adding 2with:3
adding 3with:2
adding 52with:3
expected: <25> but was: <5>
Comparison Failure:
Expected :25
Actual :5
<Click to see difference>
Note that even the fourth test case got failed, it runs the next one and report only one failure.
Junit 5 Assertions all method example
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class JUnit5AllMethodsTestCases {
@BeforeAll
static void setUpBeforeClass() {
// code to be executed before ea tests
}
@BeforeEach
void setUp() {
// code to be executed before each test
}
@Test
void testAssertArrayEquals() {
// assertArrayEquals() method test to check two arrays are equal
assertArrayEquals(new int[]{2,2,53}, new int[]{2,2,53});
}
@Test
void testAssertEquals() {
// assertEquals() method test test to check two strigns are equal
assertEquals("SLB", "SLB");
}
@Test
void testAssertFalse() {
// assertFalse() method test to test false condition
assertFalse(12 > 20);
}
@Test
void testAssertTrue() {
// assertTrue() method test to test true condition
assertTrue(120 == 120);
}
@Test
void testAssertNotNull() {
// assertNotNull() method test Not null object
assertNotNull(new Object());
}
@Test
void testAssertNull() {
// assertNull() method test null condition
assertNull(null);
}
@Test
void testAssertSame() {
// assertSame() method test two check two strings are same
String str1 = "SLB";
String str2 = "SLB";
assertSame(str1, str2);
}
@Test
void testAssertNotSame() {
// assertNotSame() method test two check two strings are not same
String str1 = "hello";
String str2 = new String("hello");
assertNotSame(str1, str2);
}
@Test
void testAssertThrows() {
// assertThrows() method test to throw exception
assertThrows(ArithmeticException.class, () -> {
int i = 1 / 0;
});
}
@Test
@Disabled("Disabled until some issues have been fixed")
void testWillBeSkipped() {
// test that will be skipped from execution
}
@AfterEach
void tearDown() {
// code to be executed after each test
}
@AfterAll
static void tearDownAfterClass() {
// code to be executed after all tests
}
}
You can check the complete example here at SLB-GITHUB