Testing that code throws the right exception under the right conditions is a critical part of robust test suites. AssertJ offers two main styles for exception assertions: assertThatThrownBy() for inline assertion chaining, and catchThrowable() for BDD-style separation of the act and assert phases.
assertThatThrownBy
Pass a lambda that is expected to throw. AssertJ captures the thrown exception and lets you assert on its type, message, cause, and more:
assertThatThrownBy(() -> Integer.parseInt("abc"))
.isInstanceOf(NumberFormatException.class)
.hasMessageContaining("abc");
catchThrowable (BDD style)
Alternatively, capture the exception into a variable using catchThrowable() and assert separately. This makes the arrange-act-assert pattern clearer:
Throwable thrown = catchThrowable(() -> Integer.parseInt("xyz"));
assertThat(thrown).isInstanceOf(NumberFormatException.class);
assertThatExceptionOfType
A third option is assertThatExceptionOfType(SomeException.class).isThrownBy(...) which is more explicit about the expected type upfront.
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class ExceptionAssertionsExample {
static int divide(int a,
int b) {
if (b == 0) throw new ArithmeticException("Division by zero");
return a / b;
}
@Test
void myTest() {
// assertThatThrownBy style
assertThatThrownBy(() -> divide(10, 0))
.isInstanceOf(ArithmeticException.class)
.hasMessage("Division by zero");
System.out.println("assertThatThrownBy passed");
// catchThrowable style
Throwable thrown = catchThrowable(() -> Integer.parseInt("abc"));
assertThat(thrown).isInstanceOf(NumberFormatException.class)
.hasMessageContaining("abc");
System.out.println("catchThrowable passed");
// assertThatExceptionOfType style
assertThatExceptionOfType(ArithmeticException.class)
.isThrownBy(() -> divide(5, 0))
.withMessage("Division by zero");
System.out.println("assertThatExceptionOfType passed");
}
}
Output$ mvn clean test -Dtest=* [INFO] Scanning for projects... [INFO] [INFO] ---------< com.logicbig.example:assertj-exception-assertions >---------- [INFO] Building assertj-exception-assertions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ assertj-exception-assertions --- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ assertj-exception-assertions --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-exception-assertions\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-exception-assertions --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-exception-assertions --- [INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-exception-assertions\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-exception-assertions --- [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ assertj-exception-assertions --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ExceptionAssertionsExample assertThatThrownBy passed catchThrowable passed assertThatExceptionOfType passed [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.092 s -- in com.logicbig.example.ExceptionAssertionsExample [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.320 s [INFO] Finished at: 2026-03-03T13:32:19+08:00 [INFO] ------------------------------------------------------------------------
Conclusion
All three exception-assertion styles succeed, confirming that the targeted code paths throw as expected. The example shows how AssertJ gives fine-grained control over verifying exception type, message text, and message containment — all without try-catch boilerplate.
Example ProjectDependencies and Technologies Used: - assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
- junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
- JDK 17
- Maven 3.9.11
|