assertThrows() method

This lesson demonstrates how to use assertThrows method in JUnit 5 to assert test conditions.

We'll cover the following

assertThrows() method

Assertions API provide static assertThrows() method. This method helps in asserting that execution of the supplied Executable throws an exception of the expectedType and returns the exception.

  • If no exception is thrown, or if an exception of a different type is thrown, this method will fail.
  • It follows the inheritance hierarchy, so the assert will pass if the expected type is Exception and actual is RuntimeException.

There are basically three useful overloaded methods for assertThrows:-

Get hands-on with 1400+ tech skills courses.