From 6cf9f8fe7bdd1024bae961a3399f276ea4292844 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco <slococo@itba.edu.ar> Date: Mon, 26 Feb 2024 14:49:08 +0100 Subject: [PATCH] Add tests --- pom.xml | 9 ++++++ src/test/java/LibraryTest.java | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/test/java/LibraryTest.java diff --git a/pom.xml b/pom.xml index c8268a4..2ebab57 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,15 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.13.1</version> + <scope>test</scope> + </dependency> + </dependencies> + <build> <plugins> <plugin> diff --git a/src/test/java/LibraryTest.java b/src/test/java/LibraryTest.java new file mode 100644 index 0000000..bcb10c1 --- /dev/null +++ b/src/test/java/LibraryTest.java @@ -0,0 +1,55 @@ +import edu.uastw.Book; +import edu.uastw.Library; +import edu.uastw.LibraryItem; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.util.List; + +import static org.junit.Assert.*; + +public class LibraryTest { + private Library library; + + @Before + public void setUp() { + library = Library.getInstance(); + } + + @After + public void tearDown() throws NoSuchFieldException, IllegalAccessException { + Field libraryItemsField = Library.class.getDeclaredField("libraryItems"); + libraryItemsField.setAccessible(true); + List<LibraryItem> libraryItems = (List<LibraryItem>) libraryItemsField.get(library); + libraryItems.clear(); + } + @Test + public void testAddItem() { + LibraryItem book = new Book("Test Book", "Test Author"); + library.addLibraryItem(book); + assertTrue(library.iterator().hasNext()); + } + + @Test + public void testIterator() { + LibraryItem book = new Book("Test Book", "Test Author"); + library.addLibraryItem(book); + assertTrue(library.iterator().hasNext()); + assertEquals(book, library.iterator().next()); + } + + @Test + public void testBooksCapacity() { + LibraryItem book1 = new Book("Book 1", "Author 1"); + LibraryItem book2 = new Book("Book 2", "Author 2"); + LibraryItem book3 = new Book("Book 3", "Author 3"); + + library.addLibraryItem(book1); + library.addLibraryItem(book2); + library.addLibraryItem(book3); + + assertEquals(3, library.getBooksCapacity()); + } +}