Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

junit5
java

How to assert that a certain exception is thrown in JUnit 5

abhilash

Overview

In JUnit 5, we use the assertThrows method to assert that a certain exception is thrown by a method/executable.

We add the following maven dependency to add JUnit 5 to the application:

<dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
</dependency>
pom.xml file

Method signature

The method asserts that the given executable throws an exception of type, expectedType, and returns the exception. This method will fail if the exception thrown is different from the specified exception or if no exception is thrown.

The method optionally takes a string message parameter that is returned as the AssertionFailedError message when the method fails.

public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable)

Parameters

  • Class<T> expectedType: This is an expected exception to be thrown by the executable.
  • Executable executable: This is a block of code that throws a Throwable.

Return value

The method returns the exception thrown by the executable.

Example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

</project>

Explanation

We define a Calculator class that contains an add method that takes two integers and returns their sum. If any integers are null, the method throws a NullPointerException.

In the above code, we concentrate on the CalculatorTest.java file.

  • Line 10: We define a string called messageIfAssertFails that is returned as the AssertionFailedError message if the assertion of the exception thrown fails.
  • Lines 11–13: We check whether invoking the add() method with one of the arguments as null throws an exception whose class is Exception. Here, we don’t check the message of the thrown exception.
  • Lines 17–24: We check whether invoking the add() method with one of the arguments as null throws an exception whose class is NullPointerException. We also check the message of the thrown exception.
  • Lines 28–33: Here an assertion exception is thrown as the Integer.parseInt() throws NumberFormatException while the expected is NullPointerException.

RELATED TAGS

junit5
java
RELATED COURSES

View all Courses

Keep Exploring