diff --git a/README.md b/README.md
index 5a65bc6..88b6eb9 100644
--- a/README.md
+++ b/README.md
@@ -186,4 +186,21 @@ graph TB
style K fill:#6cf,stroke:#f66,stroke-width:2px,stroke-dasharray: 5, 5
style L fill:#f6c,stroke:#333,stroke-width:2px
style M fill:#6fc,stroke:#f66,stroke-width:2px,stroke-dasharray: 5, 5
-```
\ No newline at end of file
+```
+
+## Test Suite
+
+The application now includes a comprehensive test suite to ensure the robustness and reliability of the codebase. The test suite is organized into separate files, each targeting specific components of the application. Below is an overview of the test files and their purposes:
+
+- `AppControllerTest.java`: Tests for the `AppController` class, covering various methods and their expected behaviors.
+- `SaleServiceTest.java`: Tests for the `SaleService` class, ensuring the correct functionality of service methods.
+- `SalesDAOTest.java`: Tests for the `SalesDAO` class, verifying data access operations.
+- `UserDetailsServiceImplTest.java`: Tests for the `UserDetailsServiceImpl` class, checking user details service methods.
+- `SecurityConfigTest.java`: Tests for the `SecurityConfig` class, validating security configuration.
+- `SessionConfigTest.java`: Tests for the `SessionConfig` class, ensuring session configuration correctness.
+- `SalesManagerTest.java`: Tests for the `SalesManager` class, covering sales management operations.
+- `UserRepositoryTest.java`: Tests for the `UserRepository` class, verifying user repository methods.
+- `SaleTest.java`: Tests for the `Sale` class, ensuring the correctness of sale-related operations.
+- `UserTest.java`: Tests for the `User` class, validating user-related operations.
+
+The test suite is integrated into the CI/CD pipeline, ensuring that all tests are executed as part of the build process. This helps in identifying and addressing issues early in the development cycle, leading to a more stable and reliable application.
diff --git a/pom.xml b/pom.xml
index 59384d5..014d1f2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,178 +1,195 @@
-
- 4.0.0
-
- org.springframework.boot
- spring-boot-starter-parent
- 2.2.2.RELEASE
-
-
- net.codejava
- salesmanager
- 0.0.9-SNAPSHOT
- demo
- Demo project for Spring Boot
-
-
- 1.8
- local.liquibase.properties
-
-
-
-
- github
- GitHub Packages
- https://maven.pkg.github.com/octodemo/java-springboot-demo
-
- true
-
-
-
-
-
-
-
- org.springframework.boot
- spring-boot-starter-jdbc
-
-
-
- org.springframework.boot
- spring-boot-starter-web
-
-
-
- org.springframework.boot
- spring-boot-starter-thymeleaf
-
-
-
- org.liquibase
- liquibase-core
- 4.23.0
-
-
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
- org.junit.vintage
- junit-vintage-engine
-
-
-
-
-
-
- org.postgresql
- postgresql
- 42.7.2
-
-
-
- com.h2database
- h2
- runtime
-
-
-
- org.hibernate.javax.persistence
- hibernate-jpa-2.1-api
- 1.0.2.Final
-
-
-
- org.springframework.boot
- spring-boot-starter-data-jpa
-
-
-
- org.springframework.security
- spring-security-config
-
-
-
- org.springframework.security
- spring-security-core
-
-
-
- org.springframework.boot
- spring-boot-starter-security
-
-
-
- org.thymeleaf.extras
- thymeleaf-extras-springsecurity5
-
-
-
- org.scala-lang
- scala-library
- 2.13.9
-
-
-
- javax.transaction
- javax.transaction-api
- 1.3
-
-
-
- org.springframework.boot
- spring-boot-starter-data-redis
-
-
-
- org.springframework.session
- spring-session-data-redis
-
-
-
- org.springframework.data
- spring-data-commons
-
-
-
-
-
-
-
- org.springframework.boot
- spring-boot-maven-plugin
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- 3.0.0-M3
-
- false
-
-
-
- org.liquibase
- liquibase-maven-plugin
- 4.23.0
-
- src/main/resources/liquibase.checks-settings.conf
- ${liquibase.propertyFile}
-
- dbchangelog.xml
- false
-
-
-
-
-
- org.postgresql
- postgresql
- 42.7.2
-
-
-
-
-
-
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.2.RELEASE
+
+
+ net.codejava
+ salesmanager
+ 0.0.9-SNAPSHOT
+ demo
+ Demo project for Spring Boot
+
+
+ 1.8
+ local.liquibase.properties
+
+
+
+
+ github
+ GitHub Packages
+ https://maven.pkg.github.com/octodemo/java-springboot-demo
+
+ true
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-jdbc
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
+
+
+ org.liquibase
+ liquibase-core
+ 4.23.0
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+
+
+
+
+
+
+ org.postgresql
+ postgresql
+ 42.7.2
+
+
+
+ com.h2database
+ h2
+ runtime
+
+
+
+ org.hibernate.javax.persistence
+ hibernate-jpa-2.1-api
+ 1.0.2.Final
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+
+ org.springframework.security
+ spring-security-config
+
+
+
+ org.springframework.security
+ spring-security-core
+
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+
+ org.thymeleaf.extras
+ thymeleaf-extras-springsecurity5
+
+
+
+ org.scala-lang
+ scala-library
+ 2.13.9
+
+
+
+ javax.transaction
+ javax.transaction-api
+ 1.3
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+
+ org.springframework.session
+ spring-session-data-redis
+
+
+
+ org.springframework.data
+ spring-data-commons
+
+
+
+ org.mockito
+ mockito-core
+ 3.11.2
+ test
+
+
+
+ org.assertj
+ assertj-core
+ 3.19.0
+ test
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0-M3
+
+ false
+
+ **/*Test.java
+
+
+
+
+ org.liquibase
+ liquibase-maven-plugin
+ 4.23.0
+
+ src/main/resources/liquibase.checks-settings.conf
+ ${liquibase.propertyFile}
+
+ dbchangelog.xml
+ false
+
+
+
+
+
+ org.postgresql
+ postgresql
+ 42.7.2
+
+
+
+
+
+
diff --git a/src/test/java/net/codejava/AppControllerTest.java b/src/test/java/net/codejava/AppControllerTest.java
new file mode 100644
index 0000000..10a5fbe
--- /dev/null
+++ b/src/test/java/net/codejava/AppControllerTest.java
@@ -0,0 +1,140 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.ui.Model;
+import org.springframework.web.servlet.mvc.support.RedirectAttributes;
+
+import javax.servlet.http.HttpSession;
+import java.security.Principal;
+import java.util.Date;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.*;
+
+public class AppControllerTest {
+
+ @Mock
+ private SalesDAO salesDAO;
+
+ @Mock
+ private AuthenticationManager authenticationManager;
+
+ @Mock
+ private Model model;
+
+ @Mock
+ private HttpSession session;
+
+ @Mock
+ private RedirectAttributes redirectAttributes;
+
+ @Mock
+ private Principal principal;
+
+ @InjectMocks
+ private AppController appController;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ public void testViewHomePage() {
+ String result = appController.viewHomePage(model, principal, 0, session);
+ assertEquals("index", result);
+ }
+
+ @Test
+ public void testShowNewForm() {
+ ModelAndView mav = appController.showNewForm();
+ assertEquals("new_form", mav.getViewName());
+ }
+
+ @Test
+ public void testShowEditForm() {
+ Sale sale = new Sale();
+ when(salesDAO.get(anyString())).thenReturn(sale);
+
+ ModelAndView mav = appController.showEditForm("123");
+ assertEquals("edit_form", mav.getViewName());
+ }
+
+ @Test
+ public void testSearch() {
+ String result = appController.search("query", model, session);
+ assertEquals("search", result);
+ }
+
+ @Test
+ public void testSave() {
+ Sale sale = new Sale();
+ sale.setDate(new Date());
+
+ String result = appController.save(sale, model, session, redirectAttributes);
+ assertEquals("redirect:/", result);
+ }
+
+ @Test
+ public void testLoginGet() {
+ String result = appController.loginGet(model);
+ assertEquals("login", result);
+ }
+
+ @Test
+ public void testLoginPost() {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ when(request.getParameter("username")).thenReturn("user");
+ when(request.getParameter("password")).thenReturn("pass");
+
+ String result = appController.loginPost(request, model);
+ assertEquals("redirect:/", result);
+ }
+
+ @Test
+ public void testUpdate() {
+ Sale sale = new Sale();
+ sale.setDate(new Date());
+
+ String result = appController.update(sale, session, redirectAttributes);
+ assertEquals("redirect:/", result);
+ }
+
+ @Test
+ public void testDelete() {
+ Sale sale = new Sale();
+ when(salesDAO.get(anyString())).thenReturn(sale);
+
+ String result = appController.delete("123", session, redirectAttributes);
+ assertEquals("redirect:/", result);
+ }
+
+ @Test
+ public void testClearRecord() {
+ Sale sale = new Sale();
+ when(salesDAO.get(anyString())).thenReturn(sale);
+
+ String result = appController.clearRecord("123", session, redirectAttributes);
+ assertEquals("redirect:/", result);
+ }
+
+ @Test
+ public void testExportToCSV() throws IOException {
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ appController.exportToCSV(response);
+ verify(response).setContentType("text/csv");
+ }
+
+ @Test
+ public void testUploadFile() {
+ MultipartFile file = mock(MultipartFile.class);
+ when(file.getInputStream()).thenReturn(new ByteArrayInputStream("Serial Number,Item Name,Amount,Quantity,Date\n123,Item,100.0,1,2021-01-01".getBytes()));
+
+ String result = appController.uploadFile(file, redirectAttributes);
+ assertEquals("redirect:/", result);
+ }
+}
diff --git a/src/test/java/net/codejava/SaleServiceTest.java b/src/test/java/net/codejava/SaleServiceTest.java
new file mode 100644
index 0000000..3a83c45
--- /dev/null
+++ b/src/test/java/net/codejava/SaleServiceTest.java
@@ -0,0 +1,44 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.*;
+
+public class SaleServiceTest {
+
+ @Mock
+ private SalesRecordRepository salesRecordRepository;
+
+ @InjectMocks
+ private SaleService saleService;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ public void testGetAllSales() {
+ Pageable pageable = PageRequest.of(0, 10);
+ Sale sale = new Sale();
+ Page salePage = new PageImpl<>(Collections.singletonList(sale));
+
+ when(salesRecordRepository.findAll(pageable)).thenReturn(salePage);
+
+ Page result = saleService.getAllSales(pageable);
+
+ assertEquals(1, result.getTotalElements());
+ verify(salesRecordRepository, times(1)).findAll(pageable);
+ }
+}
diff --git a/src/test/java/net/codejava/SaleTest.java b/src/test/java/net/codejava/SaleTest.java
new file mode 100644
index 0000000..55d2c1a
--- /dev/null
+++ b/src/test/java/net/codejava/SaleTest.java
@@ -0,0 +1,99 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Date;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class SaleTest {
+
+ @Mock
+ private Date date;
+
+ @InjectMocks
+ private Sale sale;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ sale = new Sale("123", "item", 10, 100.0f, date);
+ }
+
+ @Test
+ public void testGetSerialNumber() {
+ assertEquals("123", sale.getSerialNumber());
+ }
+
+ @Test
+ public void testSetSerialNumber() {
+ sale.setSerialNumber("456");
+ assertEquals("456", sale.getSerialNumber());
+ }
+
+ @Test
+ public void testGetItem() {
+ assertEquals("item", sale.getItem());
+ }
+
+ @Test
+ public void testSetItem() {
+ sale.setItem("newItem");
+ assertEquals("newItem", sale.getItem());
+ }
+
+ @Test
+ public void testGetQuantity() {
+ assertEquals(10, sale.getQuantity());
+ }
+
+ @Test
+ public void testSetQuantity() {
+ sale.setQuantity(20);
+ assertEquals(20, sale.getQuantity());
+ }
+
+ @Test
+ public void testGetAmount() {
+ assertEquals(100.0f, sale.getAmount());
+ }
+
+ @Test
+ public void testSetAmount() {
+ sale.setAmount(200.0f);
+ assertEquals(200.0f, sale.getAmount());
+ }
+
+ @Test
+ public void testGetDate() {
+ assertEquals(date, sale.getDate());
+ }
+
+ @Test
+ public void testSetDate() {
+ Date newDate = new Date();
+ sale.setDate(newDate);
+ assertEquals(newDate, sale.getDate());
+ }
+
+ @Test
+ public void testIsEditing() {
+ assertFalse(sale.isEditing());
+ }
+
+ @Test
+ public void testSetEditing() {
+ sale.setEditing(true);
+ assertTrue(sale.isEditing());
+ }
+
+ @Test
+ public void testToString() {
+ String expected = "Sale [serial_number=123, item=item, quantity=10, amount=100.0, date=" + date + "]";
+ assertEquals(expected, sale.toString());
+ }
+}
diff --git a/src/test/java/net/codejava/SalesManagerTest.java b/src/test/java/net/codejava/SalesManagerTest.java
new file mode 100644
index 0000000..06be5da
--- /dev/null
+++ b/src/test/java/net/codejava/SalesManagerTest.java
@@ -0,0 +1,15 @@
+package net.codejava;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+@SpringBootTest
+public class SalesManagerTest {
+
+ @Test
+ void contextLoads() {
+ SalesManager salesManager = new SalesManager();
+ assertNotNull(salesManager);
+ }
+}
diff --git a/src/test/java/net/codejava/SecurityConfigTest.java b/src/test/java/net/codejava/SecurityConfigTest.java
new file mode 100644
index 0000000..843290c
--- /dev/null
+++ b/src/test/java/net/codejava/SecurityConfigTest.java
@@ -0,0 +1,67 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.test.context.support.WithMockUser;
+import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.mockito.Mockito.*;
+
+public class SecurityConfigTest {
+
+ @Mock
+ private UserDetailsService userDetailsService;
+
+ @Mock
+ private PasswordEncoder passwordEncoder;
+
+ @Mock
+ private AuthenticationManagerBuilder authenticationManagerBuilder;
+
+ @InjectMocks
+ private SecurityConfig securityConfig;
+
+ private MockMvc mockMvc;
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ MockitoAnnotations.openMocks(this);
+ mockMvc = MockMvcBuilders
+ .standaloneSetup(new Object())
+ .apply(SecurityMockMvcRequestBuilders.springSecurity())
+ .build();
+
+ when(authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder)).thenReturn(authenticationManagerBuilder);
+ securityConfig.configureGlobal(authenticationManagerBuilder);
+ }
+
+ @Test
+ @WithMockUser
+ public void testAuthenticatedAccess() throws Exception {
+ mockMvc.perform(SecurityMockMvcRequestBuilders.formLogin("/login").user("user").password("password"))
+ .andExpect(status().isOk());
+ }
+
+ @Test
+ public void testUnauthenticatedAccess() throws Exception {
+ mockMvc.perform(SecurityMockMvcRequestBuilders.get("/"))
+ .andExpect(status().isUnauthorized());
+ }
+
+ @Test
+ public void testPermitAllAccess() throws Exception {
+ mockMvc.perform(SecurityMockMvcRequestBuilders.get("/login"))
+ .andExpect(status().isOk());
+ }
+}
diff --git a/src/test/java/net/codejava/SessionConfigTest.java b/src/test/java/net/codejava/SessionConfigTest.java
new file mode 100644
index 0000000..48c6d0a
--- /dev/null
+++ b/src/test/java/net/codejava/SessionConfigTest.java
@@ -0,0 +1,31 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.MockitoAnnotations;
+import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class SessionConfigTest {
+
+ @InjectMocks
+ private SessionConfig sessionConfig;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ public void testSessionConfigNotNull() {
+ assertNotNull(sessionConfig);
+ }
+
+ @Test
+ public void testEnableRedisHttpSessionAnnotation() {
+ EnableRedisHttpSession annotation = sessionConfig.getClass().getAnnotation(EnableRedisHttpSession.class);
+ assertNotNull(annotation);
+ }
+}
diff --git a/src/test/java/net/codejava/UserDetailsServiceImplTest.java b/src/test/java/net/codejava/UserDetailsServiceImplTest.java
new file mode 100644
index 0000000..f2b7902
--- /dev/null
+++ b/src/test/java/net/codejava/UserDetailsServiceImplTest.java
@@ -0,0 +1,77 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.crypto.password.PasswordEncoder;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+public class UserDetailsServiceImplTest {
+
+ @Mock
+ private UserRepository userRepository;
+
+ @Mock
+ private PasswordEncoder passwordEncoder;
+
+ @InjectMocks
+ private UserDetailsServiceImpl userDetailsService;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ public void testCreateUser() {
+ User user = new User();
+ user.setUsername("testuser");
+ user.setPassword("password");
+
+ when(passwordEncoder.encode(anyString())).thenReturn("encodedPassword");
+
+ userDetailsService.createUser(user);
+
+ verify(passwordEncoder, times(1)).encode("password");
+ verify(userRepository, times(1)).save(user);
+ assertEquals("encodedPassword", user.getPassword());
+ }
+
+ @Test
+ public void testLoadUserByUsername_UserExists() {
+ User user = new User();
+ user.setUsername("testuser");
+ user.setPassword("password");
+
+ when(userRepository.findByUsername("testuser")).thenReturn(user);
+
+ org.springframework.security.core.userdetails.UserDetails userDetails = userDetailsService.loadUserByUsername("testuser");
+
+ assertNotNull(userDetails);
+ assertEquals("testuser", userDetails.getUsername());
+ assertEquals("password", userDetails.getPassword());
+ }
+
+ @Test
+ public void testLoadUserByUsername_UserNotFound() {
+ when(userRepository.findByUsername("testuser")).thenReturn(null);
+
+ assertThrows(UsernameNotFoundException.class, () -> {
+ userDetailsService.loadUserByUsername("testuser");
+ });
+ }
+
+ @Test
+ public void testLoadUserByUsername_SQLGrammarException() {
+ when(userRepository.findByUsername("testuser")).thenThrow(new RuntimeException("SQL error"));
+
+ assertThrows(RuntimeException.class, () -> {
+ userDetailsService.loadUserByUsername("testuser");
+ });
+ }
+}
diff --git a/src/test/java/net/codejava/UserRepositoryTest.java b/src/test/java/net/codejava/UserRepositoryTest.java
new file mode 100644
index 0000000..5834896
--- /dev/null
+++ b/src/test/java/net/codejava/UserRepositoryTest.java
@@ -0,0 +1,75 @@
+package net.codejava;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
+
+import java.util.Optional;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+public class UserRepositoryTest {
+
+ @Mock
+ private UserRepository userRepository;
+
+ @InjectMocks
+ private UserDetailsServiceImpl userDetailsService;
+
+ private User user;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ user = new User();
+ user.setId(1L);
+ user.setUsername("testuser");
+ user.setPassword("password");
+ }
+
+ @Test
+ public void testFindByUsername() {
+ when(userRepository.findByUsername("testuser")).thenReturn(user);
+
+ User foundUser = userRepository.findByUsername("testuser");
+
+ assertThat(foundUser).isNotNull();
+ assertThat(foundUser.getUsername()).isEqualTo("testuser");
+ }
+
+ @Test
+ public void testFindById() {
+ when(userRepository.findById(1L)).thenReturn(Optional.of(user));
+
+ Optional foundUser = userRepository.findById(1L);
+
+ assertThat(foundUser).isPresent();
+ assertThat(foundUser.get().getId()).isEqualTo(1L);
+ }
+
+ @Test
+ public void testSaveUser() {
+ when(userRepository.save(user)).thenReturn(user);
+
+ User savedUser = userRepository.save(user);
+
+ assertThat(savedUser).isNotNull();
+ assertThat(savedUser.getId()).isEqualTo(1L);
+ }
+
+ @Test
+ public void testDeleteUser() {
+ userRepository.delete(user);
+
+ when(userRepository.findById(1L)).thenReturn(Optional.empty());
+
+ Optional deletedUser = userRepository.findById(1L);
+
+ assertThat(deletedUser).isNotPresent();
+ }
+}
diff --git a/src/test/java/net/codejava/UserTest.java b/src/test/java/net/codejava/UserTest.java
new file mode 100644
index 0000000..2ca07da
--- /dev/null
+++ b/src/test/java/net/codejava/UserTest.java
@@ -0,0 +1,66 @@
+package net.codejava;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.*;
+
+public class UserTest {
+
+ @Mock
+ private UserRepository userRepository;
+
+ @InjectMocks
+ private UserDetailsServiceImpl userDetailsService;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ public void testGetId() {
+ User user = new User();
+ user.setId(1L);
+ assertEquals(1L, user.getId());
+ }
+
+ @Test
+ public void testSetId() {
+ User user = new User();
+ user.setId(1L);
+ assertEquals(1L, user.getId());
+ }
+
+ @Test
+ public void testGetUsername() {
+ User user = new User();
+ user.setUsername("testuser");
+ assertEquals("testuser", user.getUsername());
+ }
+
+ @Test
+ public void testSetUsername() {
+ User user = new User();
+ user.setUsername("testuser");
+ assertEquals("testuser", user.getUsername());
+ }
+
+ @Test
+ public void testGetPassword() {
+ User user = new User();
+ user.setPassword("password");
+ assertEquals("password", user.getPassword());
+ }
+
+ @Test
+ public void testSetPassword() {
+ User user = new User();
+ user.setPassword("password");
+ assertEquals("password", user.getPassword());
+ }
+}