diff --git a/src/test/java/lambdas/LazyErrorMessage.java b/src/test/java/lambdas/LazyErrorMessage.java deleted file mode 100644 index 7985917..0000000 --- a/src/test/java/lambdas/LazyErrorMessage.java +++ /dev/null @@ -1,31 +0,0 @@ -package lambdas; - -import org.junit.jupiter.api.Test; - -import java.util.logging.Logger; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -@SuppressWarnings("Convert2MethodRef") -public class LazyErrorMessage { - private final Logger logger = Logger.getLogger(LazyErrorMessage.class.getName()); - - private String getErrorMessage() { - System.out.println("Calculating error message..."); - return "This is the error message"; - } - - @Test - void eagerErrorMessage() { - boolean x = true; - logger.fine(getErrorMessage()); - assertTrue(x, getErrorMessage()); // eagerly creates the error message - } - - @Test - void lazyErrorMessage() { - boolean x = true; - logger.fine(() -> getErrorMessage()); // Supplier - assertTrue(x, () -> getErrorMessage()); // does NOT create the error message - } -} diff --git a/src/test/java/lambdas/LazyErrorMessageTest.java b/src/test/java/lambdas/LazyErrorMessageTest.java index 4b7b681..2802b95 100644 --- a/src/test/java/lambdas/LazyErrorMessageTest.java +++ b/src/test/java/lambdas/LazyErrorMessageTest.java @@ -2,24 +2,35 @@ import org.junit.jupiter.api.Test; +import java.util.logging.Logger; + import static org.junit.jupiter.api.Assertions.assertTrue; +@SuppressWarnings("Convert2MethodRef") public class LazyErrorMessageTest { + private final Logger logger = Logger.getLogger(LazyErrorMessageTest.class.getName()); private String getErrorMessage() { - System.out.println("Generating error message..."); - return "x should be true"; + System.out.println("Calculating error message..."); + return "This is the error message"; + } + + private String getLogMessage() { + System.out.println("Calculating log message..."); + return "This is the log message"; } @Test - void assertArgIsTrue() { + void eagerErrorMessage() { boolean x = true; - assertTrue(x, getErrorMessage()); // 2nd arg is a String + logger.fine(getLogMessage()); + assertTrue(x, getErrorMessage()); // eagerly creates the error message } @Test - void assertArgIsTrue_lazyErrorMessage() { + void lazyErrorMessage() { boolean x = true; - assertTrue(x, () -> getErrorMessage()); // 2nd arg is a Supplier + logger.fine(() -> getLogMessage()); // Supplier + assertTrue(x, () -> getErrorMessage()); // does NOT create the error message } }