Junit 5 Asserts Exceptions

A good coding practice is to throw exceptions when data is invalid. A better test case is to check whether for invalid argument, the exception is thrown or not. To test both scenarios, JUnit 5 provides us assertThrows and assertDoesNotThrow method.

package org.wesome.junit5;

import java.util.Objects;

public class AppleCalculator {
    public String getError(String apple) {
        if (Objects.isNull(apple)) {
            throw new NullPointerException("apple cannot be null");
        }
        return apple;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

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

public class AppleCalculatorTest {
    @Test
    void assertThrowTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null));
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null), "it will throw exception");
        Supplier<String> messageSupplier = () -> "it will throw exception";
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null), messageSupplier);
        Exception exception = assertThrows(NullPointerException.class, () -> appleCalculator.getError(null), messageSupplier);
        assertEquals("apple cannot be null", exception.getMessage());
    }

    @Test
    void assertDoesNotThrowTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertDoesNotThrow(() -> appleCalculator.getError("apple"));
        assertDoesNotThrow(() -> appleCalculator.getError("apple"), "it will not throw exception");
        Supplier<String> messageSupplier = () -> "it will not throw exception";
        String response = assertDoesNotThrow(() -> appleCalculator.getError("apple"), messageSupplier);
        assertEquals("apple", response);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

Exception declared in assertThrows(ExceptionType.class,..) same must be thrown, if thrown exception is different or no exception is thrown at all then the test case will fail.

if assertDoesNotThrow encounters an exception, the test case will fail.

package org.wesome.junit5;

import java.util.Objects;

public class AppleCalculator {
    public Apple getError(Apple apple) {
        if (Objects.isNull(apple)) {
            throw new NullPointerException("apple cannot be null");
        }
        return apple;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class AppleCalculatorTest {
    @Test
    void assertThrowTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null));
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null), "it will throw exception");
        Supplier<String> messageSupplier = () -> "it will throw exception";
        assertThrows(IllegalArgumentException.class, () -> appleCalculator.getError(null), messageSupplier);
    }

    @Test
    void assertDoesNotThrowTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertDoesNotThrow(() -> appleCalculator.getError(new Apple()));
        assertDoesNotThrow(() -> appleCalculator.getError(new Apple()), "it will not throw exception");
        Supplier<String> messageSupplier = () -> "it will not throw exception";
        assertDoesNotThrow(() -> appleCalculator.getError(null), messageSupplier);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

Exception declared in assertThrows(ExceptionType.class,..) will incorporate its all child exception ie if parent exception is declared and test case throws child exception, still test case will be considered as pass.

package org.wesome.junit5;

import java.util.Objects;

public class AppleCalculator {
    public Apple getError(Apple apple) {
        if (Objects.isNull(apple)) {
            throw new NullPointerException("apple cannot be null");
        }
        return apple;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

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

public class AppleCalculatorTest {
    @Test
    void assertThrowTest() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null));
        assertThrows(NullPointerException.class, () -> appleCalculator.getError(null), "it will throw exception");
        Supplier<String> messageSupplier = () -> "it will throw exception";
        assertThrows(Exception.class, () -> appleCalculator.getError(null), messageSupplier);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}

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

follow us on