Assertions: assertThrows() and assertThrowsExactly()
Let's learn assertThrows() and assertThrowsExactly() assertions in JUnit 5.
The assertThrows()
method
The assertThrows()
method asserts that the execution of the given Executable
throws an exception of the expectedType
. The thrown exception is returned for further assertions.
- If the
expectedType
isException
and the actual isRuntimeException
, the assertion passes. - If no exception is thrown, or if an exception of a different type is thrown, the method fails.
Access this course and 1400+ top-rated courses and projects.