Skip to content
This repository has been archived by the owner on Dec 7, 2024. It is now read-only.

Commit

Permalink
feat(ControllerTest): create controller integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexZ7000 committed Oct 14, 2024
1 parent b093b15 commit aa2e1f7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/example/comerce/ComerceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ComerceApplication {
class ComerceApplication {

public static void main(String[] args) {
SpringApplication.run(ComerceApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.example.comerce.integration.controller;

import com.example.comerce.core.controller.UserController;
import com.example.comerce.core.entities.LoginRequest;
import com.example.comerce.core.entities.LoginResponse;
import com.example.comerce.core.entities.User;
import com.example.comerce.core.services.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
final class UserControllerIntegrationTest {

@Autowired
private MockMvc mockMvc;

@Mock
private UserService userService;

private ObjectMapper objectMapper;

private AutoCloseable closeable;

@BeforeEach
public void setUp() {
closeable = MockitoAnnotations.openMocks(this);
objectMapper = new ObjectMapper();
}

@AfterEach
public void tearDown() {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
throw new RuntimeException("Error closing resources", e);
}
}
}

@Test
public void testLogin() throws Exception {
final User user = new User();
user.setUser_id(UUID.fromString("81b9aef2-c202-4a44-9a75-d3a11afdb714"));
user.setEmail("alessandro.lima@example.com");
user.setName("Alessandro Lima");

// Token fictício
final LoginResponse loginResponse = new LoginResponse("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3Mjg5MjU5ODk5ODEsInVzZXJuYW1lIjoiYWxlc3NhbmRyby5saW1hQGV4YW1wbGUuY29tIn0=.XLeeEDpm0YGa8GAgkTU1X6KRVfnuxnMASACONjMGzoI=", user);

final LoginRequest loginRequest = new LoginRequest();
loginRequest.setEmail("alessandro.lima@example.com");
loginRequest.setPassword("testando");

when(userService.login(any(String.class), any(String.class))).thenReturn(loginResponse);

mockMvc.perform(post("/api/users/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(loginRequest)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.token").value(containsString("Bearer ")))
.andExpect(jsonPath("$.user.email").value("alessandro.lima@example.com"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@

@DataJpaTest
@Transactional
public class UserRepositoryIntegrationTest {
class UserRepositoryIntegrationTest {

@Autowired
private UserRepository userRepository;

@Test
public void testSaveAndFindUser() {
User user = new User();
public final void testSaveAndFindUser() {
final 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);
final User savedUser = userRepository.save(user);
assertNotNull(savedUser.getUser_id());

Optional<User> foundUser = userRepository.findById(savedUser.getUser_id());
final Optional<User> foundUser = userRepository.findById(savedUser.getUser_id());
assertTrue(foundUser.isPresent());
assertEquals(user.getName(), foundUser.get().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@SpringBootTest
@AutoConfigureMockMvc
@Transactional // Garante que cada teste seja executado em uma transação que será revertida
public class UserServiceIntegrationTest {
class UserServiceIntegrationTest {

@Autowired
private UserService userService;
Expand All @@ -29,28 +29,29 @@ public class UserServiceIntegrationTest {
private UUID userId;

@BeforeEach
public void setUp() {
User user = new User();
public final void setUp() {
final 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<User> foundUser = userService.findById(userId);
public final void testFindById() {
final Optional<User> foundUser = userService.findById(userId);

assertTrue(foundUser.isPresent());
assertEquals(userId, foundUser.get().getUser_id());
}

@Test
public void testDeleteUser() {
public final void testDeleteUser() {
userService.delete(userId);
Optional<User> foundUser = userService.findById(userId);
final Optional<User> foundUser = userService.findById(userId);

assertFalse(foundUser.isPresent());
}
Expand Down

0 comments on commit aa2e1f7

Please sign in to comment.