Skip to content

Commit

Permalink
Document benefits of MessageSupplier in Assertions Issue: junit-team#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitwasankar committed Jan 21, 2024
1 parent 8ebf479 commit abd72f5
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions documentation/src/test/java/example/AssertionsDemo.java
Expand Up @@ -39,6 +39,16 @@ class AssertionsDemo {
@Test
void standardAssertions() {
assertEquals(2, calculator.add(1, 1));
assertEquals(4, calculator.multiply(2, 2),
"The optional failure message is now the last parameter");
// When third parameter is used as message supplier (lambda expression)
// it will be lazily evaluated, only when the assertion fails.
assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "
+ "to avoid constructing complex messages unnecessarily.");
}

@Test
void assertWithMessageSupplier() {
// When assertion failure message is used as a plain expression,
// the expression will be evaluated irrespective of the assertion result.
assertEquals(4, calculator.multiply(2, 2),
Expand Down

0 comments on commit abd72f5

Please sign in to comment.