From 42ccad5847ec9191366c71ff6b83f57f3b8c13cc Mon Sep 17 00:00:00 2001 From: Ken Kousen Date: Tue, 17 Sep 2024 10:49:04 -0400 Subject: [PATCH] Add logging to tests in LazyErrorMessageTest Introduced the usage of `Logger` to provide additional log messages in test methods. The log messages are generated lazily in the `assertArgIsTrue_lazy` method to avoid unnecessary computation. --- src/test/java/lambdas/LazyErrorMessageTest.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/test/java/lambdas/LazyErrorMessageTest.java b/src/test/java/lambdas/LazyErrorMessageTest.java index 4b7b681..12354f3 100644 --- a/src/test/java/lambdas/LazyErrorMessageTest.java +++ b/src/test/java/lambdas/LazyErrorMessageTest.java @@ -2,24 +2,34 @@ import org.junit.jupiter.api.Test; +import java.util.logging.Logger; + import static org.junit.jupiter.api.Assertions.assertTrue; 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"; } + private String getLogMessage() { + System.out.println("Generating log message..."); + return "log message"; + } + @Test - void assertArgIsTrue() { + void assertArgIsTrue_eager() { boolean x = true; assertTrue(x, getErrorMessage()); // 2nd arg is a String + logger.fine(getLogMessage()); } @Test - void assertArgIsTrue_lazyErrorMessage() { + void assertArgIsTrue_lazy() { boolean x = true; assertTrue(x, () -> getErrorMessage()); // 2nd arg is a Supplier + logger.fine(() -> getLogMessage()); } }