The following methods are for asserting that a specific exception is thrown or not thrown during the execution of a code block.
public static <T extends Throwable> T assertThrowsExactly(Class<T> expectedType, Executable executable)
public static <T extends Throwable> T assertThrowsExactly(Class<T> expectedType, Executable executable, String message)
public static <T extends Throwable> T assertThrowsExactly(Class<T> expectedType, Executable executable, Supplier<String> messageSupplier)
public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable)
public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, String message)
public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, Supplier<String> messageSupplier)
public static void assertDoesNotThrow(Executable executable)
public static void assertDoesNotThrow(Executable executable, String message)
public static void assertDoesNotThrow(Executable executable, Supplier<String> messageSupplier)
public static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier)
public static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, String message)
public static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Supplier<String> messageSupplier)
assertThrows checks that the exception (or a subclass of the specified exception type) is thrown. In other words, it allows for subtypes of the specified exception to be thrown and still pass the assertion. assertThrowsExactly checks that the exact type of exception (and not a subclass) is thrown.
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ExceptionHandlingTest {
@Test
void testExceptionIsThrown() {
// >> Verifying that an exception is thrown
assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("Invalid argument");
});
}
@Test
void testExceptionWithMessage() {
// >> Verifying exception with a specific message
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("Invalid argument");
});
// >> Checking the exception message
assert(exception.getMessage()).equals("Invalid argument");
}
@Test
void testNoExceptionIsThrown() {
// >> Verifying no exception is thrown during code execution
assertDoesNotThrow(() -> {
String str = "Hello, World!";
// No exception here
});
}
@Test
void testExactExceptionIsThrown() {
// >> Verifying that exactly the specified exception (IllegalArgumentException) is thrown
assertThrowsExactly(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("Exact exception");
});
}
@Test
void testNoExceptionWithMethodCall() {
// >> Verifying no exception is thrown during method execution
assertDoesNotThrow(() -> {
myMethod();
});
}
// A simple method that doesn't throw an exception
void myMethod() {
System.out.println("Method executed successfully");
}
}
mvn test -Dtest=ExceptionHandlingTest OutputD:\example-projects\junit-5\junit-5-assertions\junit-5-asserting-exceptions>mvn test -Dtest=ExceptionHandlingTest [INFO] Scanning for projects... [INFO] [INFO] ---------< com.logicbig.example:junit-5-asserting-exceptions >---------- [INFO] Building junit-5-asserting-exceptions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-asserting-exceptions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-asserting-exceptions\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-asserting-exceptions --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-asserting-exceptions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-asserting-exceptions\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-asserting-exceptions --- [INFO] Recompiling the module because of added or removed source files. [INFO] Compiling 1 source file with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-asserting-exceptions --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ExceptionHandlingTest Method executed successfully [INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.052 s -- in com.logicbig.example.ExceptionHandlingTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.423 s [INFO] Finished at: 2025-12-01T13:01:23+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.8.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|
|