How to assert an exception is thrown in JUnit5 & JUnit4?

In this JUnit article, we will discuss how to assert the expected exceptions thrown by the method.

1. JUnit5 – assertThrows

JUnit5 Jupiter Assertions API introduces a static method assertThrows to assert the expected exceptions.

There are multiple overloaded methods of assertThrows. All of these methods are public static and return type of Throwable.

T assertThrows(Class expectedType, Executable executable) 
T assertThrows(Class expectedType, Executable executable, String message)
T assertThrows(Class expectedType, Executable executable, Supplier messageSupplier)

Two important parameters it expects are

  • Type of exception expected
  • An executable that is expected to throw an exception. Here we can pass the code under test as a lambda expression or as a method reference.

If the executable blocks throw the expected( or any of its child) exception then the test will pass else it will fail.

void assertNullPointerException() {
  var message = "test exception";
  var expectedException =
          () -> {
            throw new NullPointerException(message);
  assertEquals(message, expectedException.getMessage());

Since assertThrows returns a Throwable we can use it to verify the message or other details of a Throwable.

It is also important to note that expected exception is valid for the mentioned exception and all of its child classes as well.

Let’s implement the previous example by replacing the expected exception from NullPointerException with RuntimeException. New test is valid and will give the same result.

    () -> {
      throw new NullPointerException(message);

Read More: assertDoesNotThrow in JUnit5

2. JUnit4

JUnit4 provides two different ways to assert the exceptions.

Let’s discuss @Test annotation and ExpectedException rule in detail.

2.1. @Test – expected

In JUnit4, @Test annotation provides the expected attribute which can simply be used to define the exception class we are excepting.

If the executable blocks throw the expected( or any of its child) exception then the test will pass else it will fail.

@Test(expected = NullPointerException.class)
void assertNullPointerException() {
  var message = "test exception"
  throw new NullPointerException(message);

This method of asserting an exception is good if we just want to verify be exception type. To check the assertion details let’s discuss ExpectedException Rule.

2.2. ExpectedException Rule

ExpectedException is a class in JUnit4 that can be used to verify exceptions thrown by a method as well its details like the message, cause, etc.

public ExpectedException expectedException = ExpectedException.none();

public void throwsExcept() {
    var message = "test exception"


    throw new NullPointerException(message);

In the example above we have used ExpectedException to verify the type and message of the exception thrown by the test method.

Complete code samples can be found on Github project.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding.
Do not forget to share and Subscribe.
Happy coding!! 😊

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x