Contents

Junit expect exception

Writen by: David Vlijmincx

Introduction

In this article, we look at how to verify exceptions with JUnit5. We will cover how to verify an exception, verify a specific exception thrown, No exception is thrown, and assert that list of Executable's don't throw an exception.

Catch an exception with assertThrows

JUnit5 includes an Assertion that you can use to verify thrown exceptions. The method takes two parameters: the type of exception you expect and an Executable. The executable can be a Lambda expression, method reference, or implementation of the Executable interface.

In the following example, we catch the exception the Car class throws and verify the message inside the exception is what we expected it to be.

1
2
3
4
5
6
7
8
9
@Test
void throwsAnException() {
    Car car = new Car();
    RuntimeException exception = Assertions.assertThrows(RuntimeException.class, () -> {
        car.throwException();
    });

    Assertions.assertEquals("Something exception happened", exception.getMessage());
}

The test won't fail if a subtype of the exception is thrown.

Catch a specific exception with assertThrowsExactly

If you want to verify that a specific exception is thrown, you will have to use assertThrowsExactly. In the following example, we call a method that throws an EOFException. If the method throws any other exception, the test will fail.

1
2
3
4
5
6
7
8
9
@Test
void throwsAnExactlyEOFException() {
    Car car = new Car();
    EOFException eofException = Assertions.assertThrowsExactly(EOFException.class, () -> {
        car.throwsEOFException();
    });

    Assertions.assertEquals("Reached end of file", eofException.getMessage());
}

Verifying a specific exception is not thrown with assertDoesNotThrow

Yes, any execution thrown from a test method would cause a test to fail. There are cases where it can be handy to explicitly verify an exception is not thrown.

1
2
3
4
5
@Test
void DoesNotThrowAnException() {
    Car car = new Car();
    Assertions.assertDoesNotThrow(() -> car.doesNotThrowAnException());
}

Using assertAll to verify multiple Executables

JUnit also offers an Assertion to verify that multiple Executables don't throw an exception.

In the following example, we pass a list of Executables to the assertAll method that will verify that none of the lambda's will throw an exception.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@Test
void noneThrowAnException() {
    Car carOne = new Car();
    Car carTwo = new Car();
    Car carThree = new Car();
    List<Executable> executables = new ArrayList<>();

    executables.add(() -> {
        carOne.doesNotThrowAnException();
    });
    executables.add(() -> {
        carTwo.doesNotThrowAnException();
    });
    executables.add(() -> {
        carThree.doesNotThrowAnException();
    });

    Assertions.assertAll(executables.stream());

}

Conclusion

This article looked at different ways to catch exceptions and verify their content. We also looked at two cases that verify no exception is thrown.

Further reading

More about testing in Java:

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!