In JUnit 5 you test that code throws an exception with assertThrows. It takes the expected exception type and an Executable (a lambda) that should throw, and it returns the exception it caught so you can inspect the message or cause. If the lambda throws nothing, or throws a different type, the test fails.
This is the standard replacement for the JUnit 4 @Test(expected = ...) attribute and the ExpectedException rule, which no longer exist in JUnit 5.
assertThrows(Class<T> expectedType, Executable executable) runs the lambda and checks the result. Here is a small account class and a test:
The lambda has no parameters and returns nothing, so any statement that throws works. Keep the lambda to the single call you expect to fail. If you put the setup inside it, a setup bug could throw the right exception type and make the test pass for the wrong reason.
assertThrows returns the thrown exception, so you can assert on it like any other object:
For wrapped exceptions, check the cause:
Asserting the full message text makes tests brittle when wording changes. For messages that matter to users, compare the exact text; for internal ones, use assertTrue(ex.getMessage().contains("insufficient")) or check a custom exception field instead. assertInstanceOf is available in recent JUnit 5.x releases.
assertThrows passes when the thrown exception is the expected type or any subclass. That is convenient, but it can hide a mistake: expecting RuntimeException passes for almost anything. When the precise class matters, use assertThrowsExactly:
| Method | Passes when the thrown exception is |
|---|---|
assertThrows(RuntimeException.class, ...) | RuntimeException or any subclass |
assertThrowsExactly(IllegalStateException.class, ...) | Exactly IllegalStateException, not a subclass |
As a rule, assert the most specific type you can name. Asserting on Exception.class or Throwable.class rarely proves anything useful.
The opposite check is assertDoesNotThrow. It is useful when the point of the test is that a call succeeds, for example parsing a valid input:
assertDoesNotThrow also has a variant that takes a ThrowingSupplier and returns the value, so you can assert on the result afterwards. Do not wrap every normal call in it, though. A plain call that throws already fails the test with a clear stack trace.
When one operation should be rejected for several reasons, group the checks with assertAll, which runs every assertion and reports all failures together:
If you are repeating the same assertion over many inputs, a parameterized test is cleaner:
try/catch with fail() when assertThrows would do the same in one line and give a better failure message.Executable is allowed to throw Throwable, so you do not need a try/catch inside it.Exception that accepts unrelated failures, including a NullPointerException caused by a typo in the test.Also test the opposite path: for every guard clause that throws, keep at least one test where the same call with valid data succeeds.
assertThrows. The expected attribute was removed because it could not check the message and applied to the whole method, not to one call.
Yes. The lambda is an Executable, which may throw any Throwable, so checked exceptions like IOException need no extra handling.
Store the returned exception in a variable and call assertEquals on getMessage(). For partial matches, use contains.
Yes. In Kotlin, pass a lambda the same way. With Mockito you can make a mock throw using thenThrow and then check the effect with assertThrows on the code under test.
Use assertThrows(Type.class, () -> call()) to prove that code fails the way you expect, and keep the lambda limited to the failing call. Capture the returned exception to check its message or cause, prefer assertThrowsExactly when the precise class matters, and use assertDoesNotThrow for the success path. Combine these with assertAll or parameterized tests to cover many bad inputs without duplication.