diff --git a/pom.xml b/pom.xml index f4f004b..94c8976 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,12 @@ 13.0 compile - + + com.h2database + h2 + runtime + + diff --git a/src/test/java/com/example/comerce/core/entities/AddressTest.java b/src/test/java/com/example/comerce/core/entities/AddressTest.java index 0a2a1fe..a605d64 100644 --- a/src/test/java/com/example/comerce/core/entities/AddressTest.java +++ b/src/test/java/com/example/comerce/core/entities/AddressTest.java @@ -20,8 +20,9 @@ final class AddressTest { @BeforeEach public void setUp() { - final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - validator = factory.getValidator(); + try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { + validator = factory.getValidator(); + } globalAddress.setPostal_code("12345-678"); globalAddress.setStreet("Rua das Flores"); diff --git a/src/test/java/com/example/comerce/core/entities/CategoryTest.java b/src/test/java/com/example/comerce/core/entities/CategoryTest.java index 3b5a748..2194dcb 100644 --- a/src/test/java/com/example/comerce/core/entities/CategoryTest.java +++ b/src/test/java/com/example/comerce/core/entities/CategoryTest.java @@ -23,8 +23,9 @@ final class CategoryTest { @BeforeEach public void setUp() { - final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - validator = factory.getValidator(); + try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { + validator = factory.getValidator(); + } globalCategory.setDescription("Eletrônicos e Gadgets"); } diff --git a/src/test/java/com/example/comerce/core/entities/OrderTest.java b/src/test/java/com/example/comerce/core/entities/OrderTest.java index 3915355..dd454b1 100644 --- a/src/test/java/com/example/comerce/core/entities/OrderTest.java +++ b/src/test/java/com/example/comerce/core/entities/OrderTest.java @@ -25,8 +25,9 @@ final class OrderTest { @BeforeEach public void setUp() { - final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - validator = factory.getValidator(); + try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { + validator = factory.getValidator(); + } globalOrder.setDate(new Date()); globalOrder.setDiscount(100.00); diff --git a/src/test/java/com/example/comerce/core/entities/ProductTest.java b/src/test/java/com/example/comerce/core/entities/ProductTest.java index bab4d14..e5cc47e 100644 --- a/src/test/java/com/example/comerce/core/entities/ProductTest.java +++ b/src/test/java/com/example/comerce/core/entities/ProductTest.java @@ -24,8 +24,9 @@ final class ProductTest { @BeforeEach public void setUp() { - final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - validator = factory.getValidator(); + try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { + validator = factory.getValidator(); + } globalProduct.setName("Laptop"); globalProduct.setStock_quantity(10); diff --git a/src/test/java/com/example/comerce/core/entities/UserTest.java b/src/test/java/com/example/comerce/core/entities/UserTest.java index fd15a70..70cdf9e 100644 --- a/src/test/java/com/example/comerce/core/entities/UserTest.java +++ b/src/test/java/com/example/comerce/core/entities/UserTest.java @@ -25,8 +25,9 @@ final class UserTest { @BeforeEach public void setUp() { - final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - validator = factory.getValidator(); + try (final ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { + validator = factory.getValidator(); + } globalUser.setName("Alessandro Lima"); globalUser.setEmail("alessandro@email.com"); diff --git a/src/test/java/com/example/comerce/core/services/CategoryServiceTest.java b/src/test/java/com/example/comerce/core/services/CategoryServiceTest.java index 7c25e6f..b570c75 100644 --- a/src/test/java/com/example/comerce/core/services/CategoryServiceTest.java +++ b/src/test/java/com/example/comerce/core/services/CategoryServiceTest.java @@ -3,6 +3,7 @@ import com.example.comerce.core.dto.CategoryDTO; import com.example.comerce.core.entities.Category; import com.example.comerce.core.repository.CategoryRepository; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -27,9 +28,11 @@ final class CategoryServiceTest { private Category category = new Category(); private final UUID categoryId = UUID.randomUUID(); + private AutoCloseable closeable; + @BeforeEach public void setUp() { - MockitoAnnotations.openMocks(this); + closeable = MockitoAnnotations.openMocks(this); categoryDTO.setDescription("Category description"); @@ -37,6 +40,18 @@ public void setUp() { category.setCategory_id(categoryId); } + @AfterEach + public void tearDown() { + if (closeable != null) { + try { + closeable.close(); + } + catch (Exception e) { + System.err.println("Erro ao fechar os recursos: " + e.getMessage()); + } + } + } + @Test public void testFindById_Success() { when(categoryRepository.findById(categoryId)).thenReturn(Optional.of(category)); diff --git a/src/test/java/com/example/comerce/core/services/OrderServiceTest.java b/src/test/java/com/example/comerce/core/services/OrderServiceTest.java index 43410e3..ffc7a45 100644 --- a/src/test/java/com/example/comerce/core/services/OrderServiceTest.java +++ b/src/test/java/com/example/comerce/core/services/OrderServiceTest.java @@ -3,6 +3,7 @@ import com.example.comerce.core.dto.OrderDTO; import com.example.comerce.core.entities.Order; import com.example.comerce.core.repository.OrderRepository; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -30,9 +31,11 @@ final class OrderServiceTest { private Order order = new Order(); private final UUID orderId = UUID.randomUUID(); + private AutoCloseable closeable; + @BeforeEach public void setUp() { - MockitoAnnotations.openMocks(this); + closeable = MockitoAnnotations.openMocks(this); orderDTO.setDate(new Date()); orderDTO.setDiscount(100.00); @@ -42,6 +45,18 @@ public void setUp() { order.setOrder_id(orderId); } + @AfterEach + public void tearDown() { + if (closeable != null) { + try { + closeable.close(); + } + catch (Exception e) { + System.err.println("Erro ao fechar os recursos: " + e.getMessage()); + } + } + } + @Test public void testFindById_Success() { when(orderService.findById(orderId)).thenReturn(Optional.of(order)); diff --git a/src/test/java/com/example/comerce/core/services/ProductServiceTest.java b/src/test/java/com/example/comerce/core/services/ProductServiceTest.java index a29a2a7..736eabb 100644 --- a/src/test/java/com/example/comerce/core/services/ProductServiceTest.java +++ b/src/test/java/com/example/comerce/core/services/ProductServiceTest.java @@ -3,6 +3,7 @@ import com.example.comerce.core.dto.ProductDTO; import com.example.comerce.core.entities.Product; import com.example.comerce.core.repository.ProductRepository; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -30,9 +31,11 @@ final class ProductServiceTest { private Product product = new Product(); private final UUID productId = UUID.randomUUID(); + private AutoCloseable closeable; + @BeforeEach public void setUp() { - MockitoAnnotations.openMocks(this); + closeable = MockitoAnnotations.openMocks(this); productDTO.setName("Laptop"); productDTO.setStock_quantity(10); @@ -45,6 +48,11 @@ public void setUp() { product.setProduct_id(productId); } + @AfterEach + public void tearDown() throws Exception { + if (closeable != null) closeable.close(); + } + @Test public void testFindById_Success() { when(productRepository.findById(productId)).thenReturn(Optional.of(product)); diff --git a/src/test/java/com/example/comerce/core/services/UserServiceTest.java b/src/test/java/com/example/comerce/core/services/UserServiceTest.java index 6b2e85d..8cdd4c9 100644 --- a/src/test/java/com/example/comerce/core/services/UserServiceTest.java +++ b/src/test/java/com/example/comerce/core/services/UserServiceTest.java @@ -7,6 +7,7 @@ import com.example.comerce.core.entities.User; import com.example.comerce.core.repository.UserRepository; import com.example.comerce.shared.security.JWTService; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -47,9 +48,11 @@ final class UserServiceTest { private User user; private UUID userId; + private AutoCloseable closeable; + @BeforeEach public void setUp() { - MockitoAnnotations.openMocks(this); + closeable = MockitoAnnotations.openMocks(this); userId = UUID.randomUUID(); userDTO = new UserDTO(); @@ -79,6 +82,18 @@ public void setUp() { user.setUser_id(userId); } + @AfterEach + public void tearDown() { + if (closeable != null) { + try { + closeable.close(); + } + catch (Exception e) { + System.err.println("Erro ao fechar os recursos: " + e.getMessage()); + } + } + } + @Test public void testFindById_Success() { when(userRepository.findById(userId)).thenReturn(Optional.of(user)); @@ -100,13 +115,18 @@ public void testFindById_UserNotFound() { @Test public void testSaveUser() { - // Não testa a senha, pois a senha é criptografada no método save + String encodedPassword = "encodedPassword123"; + when(passwordEncoder.encode(userDTO.getPassword())).thenReturn(encodedPassword); + + user.setPassword(encodedPassword); when(userRepository.save(any(User.class))).thenReturn(user); User savedUser = userService.save(userDTO); assertNotNull(savedUser, "Não é possível salvar dados vazios"); assertEquals(user.getUser_id(), savedUser.getUser_id()); + assertEquals(encodedPassword, savedUser.getPassword(), "A senha não foi codificada corretamente"); + verify(passwordEncoder).encode(userDTO.getPassword()); } @Test diff --git a/src/test/java/com/example/comerce/integration/repository/UserRepositoryIntegrationTest.java b/src/test/java/com/example/comerce/integration/repository/UserRepositoryIntegrationTest.java new file mode 100644 index 0000000..6948ebe --- /dev/null +++ b/src/test/java/com/example/comerce/integration/repository/UserRepositoryIntegrationTest.java @@ -0,0 +1,37 @@ +package com.example.comerce.integration.repository; + +import com.example.comerce.core.entities.User; +import com.example.comerce.core.repository.UserRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +@DataJpaTest +@Transactional +public class UserRepositoryIntegrationTest { + + @Autowired + private UserRepository userRepository; + + @Test + public void testSaveAndFindUser() { + User user = new User(); + user.setName("Alessandro Lima"); + user.setEmail("alessandro@email.com"); + user.setCpf("12345678901"); + user.setTelephone("12345678901"); + user.setPassword("senha123"); + + User savedUser = userRepository.save(user); + assertNotNull(savedUser.getUser_id()); + + Optional foundUser = userRepository.findById(savedUser.getUser_id()); + assertTrue(foundUser.isPresent()); + assertEquals(user.getName(), foundUser.get().getName()); + } +} diff --git a/src/test/java/com/example/comerce/integration/services/UserServiceIntegrationTest.java b/src/test/java/com/example/comerce/integration/services/UserServiceIntegrationTest.java new file mode 100644 index 0000000..813d733 --- /dev/null +++ b/src/test/java/com/example/comerce/integration/services/UserServiceIntegrationTest.java @@ -0,0 +1,57 @@ +package com.example.comerce.integration.services; + +import com.example.comerce.core.entities.User; +import com.example.comerce.core.repository.UserRepository; +import com.example.comerce.core.services.UserService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@AutoConfigureMockMvc +@Transactional // Garante que cada teste seja executado em uma transação que será revertida +public class UserServiceIntegrationTest { + + @Autowired + private UserService userService; + + @Autowired + private UserRepository userRepository; + + private UUID userId; + + @BeforeEach + public void setUp() { + User user = new User(); + user.setName("Alessandro Lima"); + user.setEmail("alessandro@email.com"); + user.setCpf("12345678901"); + user.setTelephone("12345678901"); + user.setPassword("senha123"); + userId = userRepository.save(user).getUser_id(); + } + + @Test + public void testFindById() { + Optional foundUser = userService.findById(userId); + + assertTrue(foundUser.isPresent()); + assertEquals(userId, foundUser.get().getUser_id()); + } + + @Test + public void testDeleteUser() { + userService.delete(userId); + Optional foundUser = userService.findById(userId); + + assertFalse(foundUser.isPresent()); + } +}