Skip to content

Commit

Permalink
Add AssertAllTest and update FileFilterTest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kousen committed Apr 17, 2024
1 parent 26fff38 commit 08d6b9a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/test/java/lambdas/AssertAllTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
1 change: 0 additions & 1 deletion src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

Expand Down

0 comments on commit 08d6b9a

Please sign in to comment.