From 08d6b9ab6090b486e8b3afc4c9eadcc5913705e8 Mon Sep 17 00:00:00 2001 From: Ken Kousen Date: Wed, 17 Apr 2024 12:33:31 -0400 Subject: [PATCH] Add AssertAllTest and update FileFilterTest A new test class, AssertAllTest, has been added for testing the "Book" record functionalities. This includes tests for valid books using ISBN and invalid ISBNs. Moreover, unneeded import 'FilenameFilter' has been removed from the FileFilterTest class. --- src/test/java/lambdas/AssertAllTest.java | 38 +++++++++++++++++++++++ src/test/java/lambdas/FileFilterTest.java | 1 - 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/test/java/lambdas/AssertAllTest.java diff --git a/src/test/java/lambdas/AssertAllTest.java b/src/test/java/lambdas/AssertAllTest.java new file mode 100644 index 0000000..d8bdda4 --- /dev/null +++ b/src/test/java/lambdas/AssertAllTest.java @@ -0,0 +1,38 @@ +package lambdas; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class AssertAllTest { + + // A record is an immutable data holder + // - autogenerates equals(), hashCode(), and toString() + // - primary or "canonical" constructor goes before the braces {} + // - "getter" methods match the names of the record components, e.g., title(), author(), year() + record Book(String title, String author, int year) {} + + private Book findByIsbn(String isbn) { + if (!isbn.equals("978-0134685991")) { + throw new IllegalArgumentException("Unknown ISBN: " + isbn); + } + return new Book("Effective Java", "Joshua Bloch", 2008); + } + + @Test + public void testBook() { + Book book = findByIsbn("978-0134685991"); + assertAll( + () -> assertEquals("Effective Java", book.title()), + () -> assertEquals("Joshua Bloch", book.author()), + () -> assertEquals(2008, book.year()) + ); + } + + @Test + void testInvalidIsbn() { + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> findByIsbn("123")); + assertEquals("Unknown ISBN: 123", exception.getMessage()); + } +} diff --git a/src/test/java/lambdas/FileFilterTest.java b/src/test/java/lambdas/FileFilterTest.java index c7c1f32..76fa9ae 100644 --- a/src/test/java/lambdas/FileFilterTest.java +++ b/src/test/java/lambdas/FileFilterTest.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileFilter; -import java.io.FilenameFilter; import java.util.List; import java.util.Map;