assertThrows

assertThrows verifies that code throws an exception of a given type, by receiving the expected type and an executable lambda. It returns the exception, which lets you check its message. The test fails if no exception, or a different one, is thrown.

Example

var e = assertThrows(IllegalArgumentException.class,
    () -> new Compte(-1));

Related concepts

The whole glossary