-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters