If the body of the lambda passed to assertThrows
contains multiple statements,
execution of the lambda will stop at the first statement that throws an
exception and all subsequent statements will be ignored.
This means that:
Don’t do this:
assertThrows(
UnsupportedOperationException.class,
() -> {
AppendOnlyList list = new AppendOnlyList();
list.add(0, "a");
list.remove(0);
assertThat(list).containsExactly("a");
});
Do this instead:
AppendOnlyList list = new AppendOnlyList();
list.add(0, "a");
assertThrows(
UnsupportedOperationException.class,
() -> list.remove(0));
assertThat(list).containsExactly("a");
Suppress false positives by adding the suppression annotation @SuppressWarnings("AssertThrowsMultipleStatements")
to the enclosing element.