How to assert that a certain exception is thrown in JUnit 5
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>
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 aThrowable.
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
messageIfAssertFailsthat is returned as theAssertionFailedErrormessage if the assertion of the exception thrown fails. - Lines 11–13: We check whether invoking the
add()method with one of the arguments asnullthrows an exception whose class isException. 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 asnullthrows an exception whose class isNullPointerException. We also check the message of the thrown exception. - Lines 28–33: Here an assertion exception is thrown as the
Integer.parseInt()throwsNumberFormatExceptionwhile the expected isNullPointerException.