From 156e69b6a5ebf97b9faad8005284579401f2e2a3 Mon Sep 17 00:00:00 2001 From: Linus Samuelsson Date: Wed, 14 Jan 2026 09:45:21 +0100 Subject: [PATCH 1/2] Adds unit tests for LoanServices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dennis Seldén <111012436+dennsel@users.noreply.github.com> Co-authored-by: Linda Eskilsson Co-authored-by: Erika Falk Svendsen --- src/main/java/org/example/LoanServices.java | 2 + src/test/java/org/example/BookSearchTest.java | 2 +- .../java/org/example/LoanServicesTest.java | 75 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/example/LoanServicesTest.java diff --git a/src/main/java/org/example/LoanServices.java b/src/main/java/org/example/LoanServices.java index c5230081..78b6ea15 100644 --- a/src/main/java/org/example/LoanServices.java +++ b/src/main/java/org/example/LoanServices.java @@ -6,6 +6,8 @@ public class LoanServices { + + // Kolla om en bok är utlånad public boolean isBookLoaned(Long bookId, EntityManager em) { List loans = em.createQuery( diff --git a/src/test/java/org/example/BookSearchTest.java b/src/test/java/org/example/BookSearchTest.java index 838d7582..c45aaced 100644 --- a/src/test/java/org/example/BookSearchTest.java +++ b/src/test/java/org/example/BookSearchTest.java @@ -7,7 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; -@Disabled("Integration test – runs locally only") + class BookSearchTest { private static EntityManagerFactory emf; diff --git a/src/test/java/org/example/LoanServicesTest.java b/src/test/java/org/example/LoanServicesTest.java new file mode 100644 index 00000000..07539049 --- /dev/null +++ b/src/test/java/org/example/LoanServicesTest.java @@ -0,0 +1,75 @@ +package org.example; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; +import org.junit.jupiter.api.*; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class LoanServicesTest { + + private static EntityManagerFactory emf; + private EntityManager em; + + @BeforeAll + static void init() { + emf = Persistence.createEntityManagerFactory("library_system"); + } + + @AfterAll + static void shutdown() { + emf.close(); + } + + @BeforeEach + void setUp() { + em = emf.createEntityManager(); + } + + @AfterEach + void tearDown() { + em.close(); + } + + // test - isBookLoaned funkar utan error + @Test + void isBookLoaned_executes_without_error() { + LoanServices service = new LoanServices(); + boolean result = service.isBookLoaned(1L, em); + assertNotNull(result); + } + + // test - loanBook funkar utan error + @Test + void loanBook_executes_without_error() { + LoanServices service = new LoanServices(); + User user = em.find(User.class, 1L); + Book book = em.find(Book.class, 1L); + boolean result = service.loanBook(user, book, em); + assertNotNull(result); + } + + // test - returnBook funkar utan error + @Test + void returnBook_executes_without_error() { + LoanServices service = new LoanServices(); + + User user = em.find(User.class, 1L); + Book book = em.find(Book.class, 1L); + + boolean result = service.returnBook(user, book, em); + + assertNotNull(result); + } + + // test - activeLoans funkar utan error + @Test + void activeLoans_executes_without_error() { + LoanServices service = new LoanServices(); + List result = service.activeLoans(em.find(User.class, 1L), em); + assertNotNull(result); + } +} From bb77ee7575ba274ee31e0c861e5be01a0729c082 Mon Sep 17 00:00:00 2001 From: Linus Samuelsson Date: Wed, 14 Jan 2026 09:53:28 +0100 Subject: [PATCH 2/2] Disables integration tests for LoanServicesTest and BookSearchTest --- src/test/java/org/example/BookSearchTest.java | 3 ++- src/test/java/org/example/LoanServicesTest.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/example/BookSearchTest.java b/src/test/java/org/example/BookSearchTest.java index c45aaced..090c05c0 100644 --- a/src/test/java/org/example/BookSearchTest.java +++ b/src/test/java/org/example/BookSearchTest.java @@ -7,9 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; - +@Disabled("Integration test – runs locally only") class BookSearchTest { + private static EntityManagerFactory emf; private EntityManager em; diff --git a/src/test/java/org/example/LoanServicesTest.java b/src/test/java/org/example/LoanServicesTest.java index 07539049..6d8d31fa 100644 --- a/src/test/java/org/example/LoanServicesTest.java +++ b/src/test/java/org/example/LoanServicesTest.java @@ -8,7 +8,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertNotNull; - +@Disabled("Integration test – runs locally only") public class LoanServicesTest { private static EntityManagerFactory emf;