1. Overview
2. JUnit 5 Exception Testing
Let’s assume that we have a class StringUtils that has a method convertToInt to convert a given string into Integer. If the given string is Null or empty, then the method will throw an IllegalArgumentException with a message: “String must be not null or empty“.
public final class StringUtils {
public static Integer convertToInt(String str) {
if (str == null || str.trim().length() == 0) {
throw new IllegalArgumentException("String must be not null or empty");
}
return Integer.valueOf(str);
}
}
We write some tests for this method to get to know about JUnit 5 exception testing.
2.1. Using assertThrows annotation.
Let’s see the syntax of assertThrows in JUnit 5. This method is used to assert that the supplied executable will throw an exception of the expectedType. If there is no exception of expectedType is thrown, the method will fail.
public static void assertThrows(Class<? extends Throwable> expectedType, Executable executable) {
// Omitted content for short
}
For example, we will write a test method which we will call the StringUtils.convertToInt method and pass to it a Null parameter.
@RunWith(JUnitPlatform.class)
public class StringUtilsTestUnit5Exception {
@Test
public void convertToIntNullParameterAssertThrows() {
String st = null;
assertThrows(IllegalArgumentException.class, () -> {
StringUtils.convertToInt(st);
});
}
}
2.2. Using expectThrows annotation
Let’s see the syntax of expectThrows in JUnit 5.
public static <T extends Throwable> T expectThrows(Class<T> expectedType, Executable executable) {
//Omitted content for short
}
The expectedThrows method is almost similar to the assertThrows, except that this method returns the thrown exception.
For example, let’s try to test our method.
public class StringUtilsTestUnit5Exception {
@Test
public void convertToIntNullParameterExpectThrows() {
String st = null;
Throwable exception = expectThrows(IllegalArgumentException.class, () -> {
StringUtils.convertToInt(st);
});
assertEquals("String must be not null or empty", exception.getMessage());
}
}
We can get back the exception and try to compare our expected error message with the actual message. The expectThrows is used to verify more detail of the thrown exception rather than just the type of exception as assertThrows method.
2.3. Using Try/Catch Idiom
One more way to do with JUnit 5 exception testing is to use try/catch idiom likes the previous version of JUnit.
Let’s see an example to test our convertToInt method.
@Test
public void convertToIntNullParameterTryCatchIdiom() {
String st = null;
try {
StringUtils.convertToInt(st);
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException e) {
assertEquals("String must be not null or empty", e.getMessage());
}
}
3. Complete Example For Reference
package com.javadevelopersguide.junit5.exception;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.javadevelopersguide.junit5.StringUtils;
@RunWith(JUnitPlatform.class)
public class StringUtilsTestUnit5Exception {
@Test
public void convertToIntNullParameterAssertThrows() {
String st = null;
assertThrows(IllegalArgumentException.class, () -> {
StringUtils.convertToInt(st);
});
}
@Test
public void convertToIntNullParameterExpectThrows() {
String st = null;
Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
StringUtils.convertToInt(st);
});
assertEquals("String must be not null or empty", exception.getMessage());
}
@Test
public void convertToIntNullParameterTryCatchIdiom() {
String st = null;
try {
StringUtils.convertToInt(st);
fail("Expected an IllegalArgumentException to be thrown");
} catch (IllegalArgumentException e) {
assertEquals("String must be not null or empty", e.getMessage());
}
}
}
4. Conclusion
We have learned about JUnit 5 exception testing, how to use basic assertions like expectThrows and assertThrows to test exceptions. In the next posts, I’d like to share more about the new features of JUnit 5.
Reference: https://junit.org/junit5/docs/current/user-guide/
Reference: https://junit.org/junit5/docs/current/user-guide/
JUnit 5 Related Posts
- Overview of JUnit 5
- JUnit 5 Maven Example
- JUnit 5 Standard Test Class Basic Template
- JUnit 5 Annotations with Examples
- JUnit 5 Assertions with Examples
- JUnit 5 Nested Tests Example
- JUnit 5 Disabling Tests Examples
- JUnit 5 Display Names Example
- JUnit 5 Repeated Tests with Examples
- JUnit 5 Exception Testing with Example
- Overview of JUnit 5
- JUnit 5 Maven Example
- JUnit 5 Standard Test Class Basic Template
- JUnit 5 Annotations with Examples
- JUnit 5 Assertions with Examples
- JUnit 5 Nested Tests Example
- JUnit 5 Disabling Tests Examples
- JUnit 5 Display Names Example
- JUnit 5 Repeated Tests with Examples
- JUnit 5 Exception Testing with Example
I like to learn a piece of new information about technology. Im really like your post. Good job.
ReplyDeleteStruts Training in Chennai
struts course
Struts Training in Tambaram
Wordpress Training in Chennai
Wordpress Training Chennai
Spring Training in Chennai
Hibernate Training in Chennai
Struts Training in Chennai