Junit 5 Asserts

Asserts are the core heart of all test cases. it needs to be evaluated as true in order for test cases to pass. JUnit 5 provides a lot of new assert over JUnit 4. Asserts can be used with Java 8 lambda also. All assets of Junit 5 are static methods and are defined in org.junit.jupiter.api.Assertions class.
we have multiple ways to validate a response like assertTrue, assertFalse, assertNull, assertNotNull, assertEquals, assertNotEquals, assertArrayEquals, assertSame, assertNotSame or assertAll to validate multiple responses together.
All assert methods have multiple overloaded methods for all data types ie int, short, float, char, etc.

All assertions of JUnit 5 have the same structure.

  • public static void assert...(expected, actual)
  • public static void assert...(expected, actual, String message)
  • public static void assert...(expected, actual, Supplier<String> messageSupplier)

Assert True

assertTrue indicates that if the result return from the method is true then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public boolean booleanApple(String appleText) {
        return "apple".equals(appleText);
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertTrue;

class AppleCalculatorTest {
    @Test
    void assertTrueTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertTrue(appleCalculator.booleanApple("apple"));
        assertTrue(appleCalculator.booleanApple("apple"), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertTrue(appleCalculator.booleanApple("apple"), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

Assert False

assertFalse indicates that if the result return from the method is false then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public boolean booleanApple(String appleText) {
        return "apple".equals(appleText);
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertFalse;

class AppleCalculatorTest {
    @Test
    void assertFalseTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertFalse(appleCalculator.booleanApple("apple"));
        assertFalse(appleCalculator.booleanApple("apple"), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertFalse(appleCalculator.booleanApple("apple"), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert Null

assertNull indicates that if the result return from the method is null then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public Object booleanApple(String appleText) {
        if ("apple".equals(appleText)) {
            return null;
        } else {
            return new Object();
        }
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertNull;

class AppleCalculatorTest {
    @Test
    void assertFalseTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertNull(appleCalculator.booleanApple("apple"));
        assertNull(appleCalculator.booleanApple("apple"), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertNull(appleCalculator.booleanApple("apple"), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

Assert Not Null

assertNotNull indicates that if the result return from the method is not null then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public Object booleanApple(String appleText) {
        if ("apple".equals(appleText)) {
            return null;
        } else {
            return new Object();
        }
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class AppleCalculatorTest {
    @Test
    void assertNotNullTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertNotNull(appleCalculator.booleanApple("orange"));
        assertNotNull(appleCalculator.booleanApple("orange"), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertNotNull(appleCalculator.booleanApple("orange"), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert Equals

assertEquals indicate that if the actual result return from the method is equal to the excepted result then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    void assertEqualsTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1));
        assertEquals(2, appleCalculator.addApple(1, 1), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertEquals(2, appleCalculator.addApple(1, 1), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert Not Equals

assertNotEquals indicate that if the actual result return from the method is equal to the excepted result then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertNotEquals;

class AppleCalculatorTest {
    @Test
    void assertNotEqualsTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertNotEquals(1, appleCalculator.addApple(1, 1));
        assertNotEquals(1, appleCalculator.addApple(1, 1), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertNotEquals(1, appleCalculator.addApple(1, 1), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert Equals with Delta

In Different machines or underlaying architecture, we might get different decimal numbers which might lead to our test case failure, to incorporate that assertEquals for float and double takes an extra value of delta, the result returned within the delta of the expected result will still consider as passed.

package com.example.junit5.sujan;

public class AppleCalculator {
    public float addAppleFloat(int apple1, int apple2) {
        return (float) apple1 + apple2;
    }

    public double addAppleDouble(int apple1, int apple2) {
        return (float) apple1 + apple2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    void assertEqualsTestFloat() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2.5f, appleCalculator.addAppleFloat(1, 1), 0.5);
        assertEquals(2.5f, appleCalculator.addAppleFloat(1, 1), 0.5, "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertEquals(2.5f, appleCalculator.addAppleFloat(1, 1), 0.5, messageSupplier);
    }

    @Test
    void assertEqualsDouble() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2.5d, appleCalculator.addAppleDouble(1, 1), 0.5);
        assertEquals(2.5d, appleCalculator.addAppleDouble(1, 1), 0.5, "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertEquals(2.5d, appleCalculator.addAppleDouble(1, 1), 0.5, messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

Assert Same

assertSame indicate that if the actual result return from the method is the same as the excepted result then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public String getApple() {
        return "apple";
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertSame;

class AppleCalculatorTest {
    @Test
    void assertSameTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertSame("apple", appleCalculator.getApple());
        assertSame("apple", appleCalculator.getApple(), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertSame("apple", appleCalculator.getApple(), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert Not Same

assertNotSame indicates that if the actual result return from the method is the same as the excepted result then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public String getApple() {
        return "apple";
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertNotSame;

class AppleCalculatorTest {
    @Test
    void assertNotSameTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertNotSame("orange", appleCalculator.getApple());
        assertNotSame("orange", appleCalculator.getApple(), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertNotSame("orange", appleCalculator.getApple(), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert Array Equals

assertArrayEquals indicate that if the actual array return from the method is the same as an excepted array then the test will be considered as pass.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int[] addAppleArray() {
        return new int[]{2 / 2, 4 / 2, 6 / 2};
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

class AppleCalculatorTest {
    @Test
    void assertArrayEqualsTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertArrayEquals(new int[]{1, 2, 3}, appleCalculator.addAppleArray());
        assertArrayEquals(new int[]{1, 2, 3}, appleCalculator.addAppleArray(), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertArrayEquals(new int[]{1, 2, 3}, appleCalculator.addAppleArray(), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

Assert Iterable Equals

assertIterableEquals compares the actual iterable object ie Array or collection, must be deep equal to the expected iterable result. Expected and actual iterable objects must have the same count as the object and must be equal per position as well.

package com.example.junit5.sujan;

import java.util.ArrayList;
import java.util.List;

public class AppleCalculator {
    public List<String> getApple() {
        List<String> appleList = new ArrayList<>();
        appleList.add("Macintosh");
        appleList.add("Fuji");
        appleList.add("Gala");
        appleList.add("Jonagold");
        appleList.add("GrannySmith");
        appleList.add("PinkLady");
        return appleList;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertIterableEquals;

class AppleCalculatorTest {
    @Test
    void assertIterableEqualsTest() {
        List<String> appleList = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold", "GrannySmith", "PinkLady");
        AppleCalculator appleCalculator = new AppleCalculator();
        assertIterableEquals(appleList, appleCalculator.getApple());
        assertIterableEquals(appleList, appleCalculator.getApple(), "we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        assertIterableEquals(appleList, appleCalculator.getApple(), messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert fail

we need to fail the test case no matter what, for this, we can use assertFail. it will fail the test case irrespective of the result.

package com.example.junit5.sujan;

public class AppleCalculator {
    public String getApple() {
        return "apple";
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.fail;

class AppleCalculatorTest {
    @Test
    void failTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        fail();
        fail(appleCalculator.getApple());
        fail("we love apple");
        Supplier<String> messageSupplier = () -> "we love apple";
        fail(messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

Assert All

assertAll is a lambda function which takes Executable as a parameter, it can assert multiple results, if all asserts passed then only test will be considered as passed.

package com.example.junit5.sujan;

public class Apple {
    private int appleId;
    private String appleName;

    public Apple(int appleId, String appleName) {
        this.appleId = appleId;
        this.appleName = appleName;
    }

    public int getAppleId() {
        return appleId;
    }

    public String getAppleName() {
        return appleName;
    }
}
package com.example.junit5.sujan;

public class AppleCalculator {
    public Apple getApple() {
        Apple apple = new Apple(1, "apple");
        return apple;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class AppleCalculatorTest {
    @Test
    void assertAllTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        Apple apple = appleCalculator.getApple();
        assertAll(() -> assertNotNull(apple),
                () -> assertNotNull(apple, "apple is not null"),
                () -> assertEquals(1, apple.getAppleId()),
                () -> assertEquals(1, apple.getAppleId(), "apple id is 1"),
                () -> assertEquals("apple", apple.getAppleName()),
                () -> assertEquals("apple", apple.getAppleName(), "apple name is apple")
        );
        Supplier<String> messageSupplier = () -> "apple is not null";
        assertNotNull(apple, messageSupplier);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

follow us on