Skip to content

Commit

Permalink
Add logging to tests in LazyErrorMessageTest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kousen committed Sep 17, 2024
1 parent 99a6ae1 commit 42ccad5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/test/java/lambdas/LazyErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
logger.fine(() -> getLogMessage());
}
}

0 comments on commit 42ccad5

Please sign in to comment.