From 52a8dfa36414904e5308d71efed0b11dec61c90b Mon Sep 17 00:00:00 2001 From: Ankit Wasankar Date: Wed, 10 Jan 2024 19:42:22 +0530 Subject: [PATCH] Document benefits of MessageSupplier in Assertions Issue: #3153 --- .../src/test/java/example/AssertionsDemo.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/documentation/src/test/java/example/AssertionsDemo.java b/documentation/src/test/java/example/AssertionsDemo.java index 0feb7740cb70..42091cc740dc 100644 --- a/documentation/src/test/java/example/AssertionsDemo.java +++ b/documentation/src/test/java/example/AssertionsDemo.java @@ -39,10 +39,14 @@ class AssertionsDemo { @Test void standardAssertions() { assertEquals(2, calculator.add(1, 1)); + // 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), - "The optional failure message is now the last parameter"); - assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- " - + "to avoid constructing complex messages unnecessarily."); + "This expression will always evaluate irrespective of assertion result.") + // When assertion failure message is created as a lambda expression, + // expression will only evaluate when assertion result is failed (lazy evaluation) + // and can help us save unnecessary computations when they are not needed. + assertTrue('a' < 'b', () -> "This lambda will evaluate only if assertion fails."); } @Test