-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AdrianBalunan
committed
Apr 24, 2026
1 parent
ce7e698
commit f467510
Showing
1 changed file
with
72 additions
and
1 deletion.
There are no files selected for viewing
73 changes: 72 additions & 1 deletion
73
...elpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAOTest.java
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 |
|---|---|---|
| @@ -1,4 +1,75 @@ | ||
| package ntnu.systemutvikling.team6.database.DAO; | ||
|
|
||
| import ntnu.systemutvikling.team6.database.DatabaseConnection; | ||
| import org.junit.jupiter.api.*; | ||
| import java.sql.*; | ||
| import java.util.List; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class CategoryDAOTest { | ||
| } | ||
|
|
||
| // --- Mocks --- | ||
| private DatabaseConnection mockDbConnection; | ||
| private Connection mockConn; | ||
| private Statement mockStmt; | ||
| private ResultSet mockRs; | ||
|
|
||
| private CategoryDAO categoryDAO; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws SQLException { | ||
| mockDbConnection = mock(DatabaseConnection.class); | ||
| mockConn = mock(Connection.class); | ||
| mockStmt = mock(Statement.class); | ||
| mockRs = mock(ResultSet.class); | ||
|
|
||
| when(mockDbConnection.getMySqlConnection()).thenReturn(mockConn); | ||
| when(mockConn.createStatement()).thenReturn(mockStmt); | ||
| when(mockStmt.executeQuery(anyString())).thenReturn(mockRs); | ||
|
|
||
| categoryDAO = new CategoryDAO(mockDbConnection); | ||
| } | ||
|
|
||
| @Test | ||
| void getCategoriesFromDB_returnsAllCategories() throws SQLException { | ||
| when(mockRs.next()).thenReturn(true, true, true, false); | ||
| when(mockRs.getString("category")).thenReturn("Education", "Health", "Youth"); | ||
|
|
||
| List<String> result = categoryDAO.getCategoriesFromDB(); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(3, result.size()); | ||
| assertTrue(result.contains("Education")); | ||
| assertTrue(result.contains("Health")); | ||
| assertTrue(result.contains("Youth")); | ||
| } | ||
|
|
||
| @Test | ||
| void getCategoriesFromDB_returnsEmptyListWhenNoCategories() throws SQLException { | ||
| when(mockRs.next()).thenReturn(false); | ||
|
|
||
| List<String> result = categoryDAO.getCategoriesFromDB(); | ||
|
|
||
| assertNotNull(result); | ||
| assertTrue(result.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void getCategoriesFromDB_returnsSingleCategory() throws SQLException { | ||
| when(mockRs.next()).thenReturn(true, false); | ||
| when(mockRs.getString("category")).thenReturn("Environment"); | ||
|
|
||
| List<String> result = categoryDAO.getCategoriesFromDB(); | ||
|
|
||
| assertEquals(1, result.size()); | ||
| assertEquals("Environment", result.getFirst()); | ||
| } | ||
|
|
||
| @Test | ||
| void getCategoriesFromDB_throwsRuntimeExceptionOnSQLException() throws SQLException { | ||
| when(mockConn.createStatement()).thenThrow(new SQLException("DB down")); | ||
|
|
||
| assertThrows(RuntimeException.class, () -> categoryDAO.getCategoriesFromDB()); | ||
| } | ||
| } |