Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

Simplified Learning Blog

Learning made easy

  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • Spring Boot Tutorial
      • Spring Boot Rest API Example complete guide
    • Top 50 Java Interview Questions
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
    • Java Thread Tutorials
      • How to create thread in Java
      • Multithreading in java
      • Daemon Thread in Java | How to create daemon thread in java
      • Top 40+ Multithreading interview questions
  • AWS
    • What is AWS (Amazon Web Services)
    • AWS IAM (Identity and Access Management)
    • AWS SNS | What is SNS
    • What is SQS | AWS SQS (Simple Queue Service)
    • What is AWS Lambda
    • Top 10 AWS Lambda interview questions
  • Java Codes
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • System Performance Objective
  • Spring Boot Tutorial
  • Tools
    • JSON Formatter and Validator
  • Tech Blogs
    • Java 21 New Features
    • Is Java Dead? Is java dead, 2023 ?
    • New Features in Java 17
  • Toggle search form

Assertall in JUnit 5

Posted on March 1, 2023April 14, 2023 By Admin No Comments on Assertall in JUnit 5

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.

Table of Contents

Toggle
  • What is assertall in Junit 5 ?
  • assertAll in JUnit 5 Example:
  • Junit 5 Assertions all method example

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
Assertall in JUnit 5
Junit 5 AssertAll() example

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>
Assertall in JUnit 5
Junit 5 Assertions

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

Related

Java

Post navigation

Previous Post: Type Casting in Java | 2 types Implicit and explicit casting
Next Post: Software Architecture Performance

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • PDF to JSON Convertor
  • System Performance Objective
  • Performance Principles of Software Architecture
  • Calculate date of birth from age in jquery
  • How to convert excel to PDF using java

Recent Comments

No comments to show.

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook WordPress theme