diff --git a/src/test/java/lambdas/FileFilterTest.java b/src/test/java/lambdas/FileFilterTest.java index a77f821..7e2b15e 100644 --- a/src/test/java/lambdas/FileFilterTest.java +++ b/src/test/java/lambdas/FileFilterTest.java @@ -6,8 +6,10 @@ import java.io.File; import java.io.FileFilter; +import java.io.FilenameFilter; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) public class FileFilterTest { @@ -39,6 +41,13 @@ void listDirectories_expressionLambda() { assertEquals(14, directories.length); } + @Test + void listDirectories_method_reference() { + File[] directories = root.listFiles(File::isDirectory); + assert directories != null; + assertEquals(14, directories.length); + } + @Test void listDirectories_blockLambda() { File[] directories = root.listFiles(path -> { @@ -55,4 +64,26 @@ void list_directories_assigned_to_variable() { assert directories != null; assertEquals(14, directories.length); } + + @Test + void expect_exception() { + String x = null; + Exception ex = assertThrows(NullPointerException.class, + () -> x.length()); + System.out.println(ex.getMessage()); + } + + @Test + void list_directories_java_src_files_filenamefilter() { + File[] directories = root.listFiles((dir, name) -> name.endsWith(".java")); + assertNotNull(directories); + assertEquals(8, directories.length); + } + + @Test + void list_directories_java_src_files_filefilter() { + File[] directories = root.listFiles(path -> path.getName().endsWith(".java")); + assertNotNull(directories); + assertEquals(8, directories.length); + } } diff --git a/src/test/java/lambdas/LazyErrorMessageTest.java b/src/test/java/lambdas/LazyErrorMessageTest.java index 4b7b681..05e142b 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 log = Logger.getLogger(this.getClass().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 "x should be true"; + } + @Test void assertArgIsTrue() { boolean x = true; assertTrue(x, getErrorMessage()); // 2nd arg is a String + log.fine(getLogMessage()); } @Test void assertArgIsTrue_lazyErrorMessage() { boolean x = true; assertTrue(x, () -> getErrorMessage()); // 2nd arg is a Supplier + log.fine(() -> getLogMessage()); } }