Skip to content

Commit

Permalink
Add new tests in FileFilterTest and LazyErrorMessageTest
Browse files Browse the repository at this point in the history
Two new tests were added. In FileFilterTest.java, a test for listDirectories_variable and mapForEach was added to validate functionalities. Meanwhile, in LazyErrorMessageTest.java, a Logger was introduced to generate a fine log message during assertion tests.
  • Loading branch information
kousen committed May 21, 2024
1 parent 7d551c0 commit 196f971
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.FileFilter;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -53,4 +54,20 @@ void listDirectories_blockLambda() {
.isNotEmpty()
.hasSize(14);
}

@Test
void listDirectories_variable() {
FileFilter filter = (File pathname) -> pathname.isDirectory();
File[] directories = root.listFiles(filter);
assertThat(directories)
.isNotNull()
.isNotEmpty()
.hasSize(14);
}

@Test
void mapForEach() {
Map.of(1, "one", 2, "two", 3, "three")
.forEach((key, value) -> System.out.println(key + " -> " + value));
}
}
10 changes: 10 additions & 0 deletions src/test/java/lambdas/LazyErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

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 log = Logger.getLogger(getClass().getName());

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

private String getErrorMessage() {
System.out.println("Generating error message...");
Expand All @@ -14,12 +22,14 @@ private String getErrorMessage() {
@Test
void assertArgIsTrue() {
boolean x = true;
log.fine(getLogMessage());
assertTrue(x, getErrorMessage()); // 2nd arg is a String
}

@Test
void assertArgIsTrue_lazyErrorMessage() {
boolean x = true;
log.fine(() -> getLogMessage());
assertTrue(x, () -> getErrorMessage()); // 2nd arg is a Supplier<String>
}
}

0 comments on commit 196f971

Please sign in to comment.