-
Notifications
You must be signed in to change notification settings - Fork 0
Adds unit tests for LoanServices #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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; | ||||||||||||||||||||||||||||||||||||||||||
| @Disabled("Integration test – runs locally only") | ||||||||||||||||||||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested fix void isBookLoaned_executes_without_error() {
LoanServices service = new LoanServices();
- boolean result = service.isBookLoaned(1L, em);
- assertNotNull(result);
+ Book book = em.find(Book.class, 1L);
+ Assertions.assertDoesNotThrow(() -> service.isBookLoaned(book != null ? book.getId() : 1L, em));
}Or if you want to verify the actual value: boolean result = service.isBookLoaned(1L, em);
assertFalse(result); // or assertTrue, depending on expected state🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // 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); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential
Suggested fix `@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);
+ assertNotNull(user, "Test requires User with id=1 in database");
+ assertNotNull(book, "Test requires Book with id=1 in database");
+ Assertions.assertDoesNotThrow(() -> service.loanBook(user, book, em));
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // 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); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+57
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issues: potential Apply the same fixes as Suggested fix `@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);
+ assertNotNull(user, "Test requires User with id=1 in database");
+ assertNotNull(book, "Test requires Book with id=1 in database");
+ Assertions.assertDoesNotThrow(() -> service.returnBook(user, book, em));
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // test - activeLoans funkar utan error | ||||||||||||||||||||||||||||||||||||||||||
| @Test | ||||||||||||||||||||||||||||||||||||||||||
| void activeLoans_executes_without_error() { | ||||||||||||||||||||||||||||||||||||||||||
| LoanServices service = new LoanServices(); | ||||||||||||||||||||||||||||||||||||||||||
| List<Loan> result = service.activeLoans(em.find(User.class, 1L), em); | ||||||||||||||||||||||||||||||||||||||||||
| assertNotNull(result); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+68
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential
Suggested fix `@Test`
void activeLoans_executes_without_error() {
LoanServices service = new LoanServices();
- List<Loan> result = service.activeLoans(em.find(User.class, 1L), em);
+ User user = em.find(User.class, 1L);
+ assertNotNull(user, "Test requires User with id=1 in database");
+ List<Loan> result = service.activeLoans(user, em);
assertNotNull(result);
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null check before closing
emffor consistency and safety.If
init()fails,emfwill benullandshutdown()will throw aNullPointerException. TheBookSearchTestclass guards against this.Suggested fix
`@AfterAll` static void shutdown() { - emf.close(); + if (emf != null) emf.close(); }🤖 Prompt for AI Agents