Skip to content

Commit

Permalink
Add unit tests for lazy vs eager message evaluation
Browse files Browse the repository at this point in the history
Introduce `LazyMessageTest` to compare eager and lazy message evaluation in assertions and logging. This provides better insights into optimization and ensures correct behavior when using lambda expressions for deferred execution.
  • Loading branch information
kousen committed Dec 11, 2024
1 parent 1c70e0f commit 6fb07f4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/test/java/lambdas/LazyMessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class LazyMessageTest {
private final Logger logger = Logger.getLogger(LazyMessageTest.class.getName());

private String getErrorMessage() {
System.out.println("Getting error message");
return "Error message";
}

private String getLogMessage() {
System.out.println("Getting log message");
return "Log message";
}

@Test
void eagerMessage() {
boolean x = true;
assertTrue(x, getErrorMessage());
logger.fine(getLogMessage());
}

@Test
void lazyMessage() {
boolean x = true;
assertTrue(x, () -> getErrorMessage());
logger.fine(() -> getLogMessage());
}
}

0 comments on commit 6fb07f4

Please sign in to comment.