Skip to content

Commit

Permalink
Rename class and update test cases to enhance readability
Browse files Browse the repository at this point in the history
Renamed the class LazyErrorMessage to LazyErrorMessageTest for better readability and to accurately reflect its functionality as a test class. Deleted the original LazyErrorMessageTest and refactored its methods into LazyErrorMessage. The methods have been updated to use lambdas for lazy error message generation, improving their efficiency. The changes are meant to enhance code readability and promote efficient use of resources.
  • Loading branch information
kousen committed Nov 9, 2023
1 parent c152d17 commit 57d4afd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
31 changes: 0 additions & 31 deletions src/test/java/lambdas/LazyErrorMessage.java

This file was deleted.

23 changes: 17 additions & 6 deletions src/test/java/lambdas/LazyErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
logger.fine(() -> getLogMessage()); // Supplier<String>
assertTrue(x, () -> getErrorMessage()); // does NOT create the error message
}
}

0 comments on commit 57d4afd

Please sign in to comment.