test: add integration tests for TodoController and update test configuration.
All checks were successful
Release and Build Docker Images / release-and-build (push) Successful in 1m26s
All checks were successful
Release and Build Docker Images / release-and-build (push) Successful in 1m26s
This commit is contained in:
@@ -2,8 +2,10 @@ package com.todo.backend;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
class BackendApplicationTests {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.todo.backend.controller;
|
||||
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
import com.todo.backend.model.Todo;
|
||||
import com.todo.backend.service.TodoService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
|
||||
// Spring Boot 3.4+ replaces @MockBean with @MockitoBean
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@WebMvcTest(TodoController.class)
|
||||
public class TodoControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@MockitoBean
|
||||
private TodoService todoService;
|
||||
|
||||
private Todo todo1;
|
||||
private Todo todo2;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
todo1 = new Todo("Test Todo 1", "Description 1", false);
|
||||
todo1.setId(1L);
|
||||
todo2 = new Todo("Test Todo 2", "Description 2", true);
|
||||
todo2.setId(2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllTodos_ReturnsListOfTodos() throws Exception {
|
||||
List<Todo> todos = Arrays.asList(todo1, todo2);
|
||||
when(todoService.getAllTodos()).thenReturn(todos);
|
||||
|
||||
mockMvc.perform(get("/api/todos"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(2)))
|
||||
.andExpect(jsonPath("$[0].title", is(todo1.getTitle())))
|
||||
.andExpect(jsonPath("$[1].title", is(todo2.getTitle())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTodoById_WhenExists_ReturnsTodo() throws Exception {
|
||||
when(todoService.getTodoById(1L)).thenReturn(Optional.of(todo1));
|
||||
|
||||
mockMvc.perform(get("/api/todos/1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.title", is(todo1.getTitle())))
|
||||
.andExpect(jsonPath("$.id", is(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTodoById_WhenNotExists_ReturnsNotFound() throws Exception {
|
||||
when(todoService.getTodoById(99L)).thenReturn(Optional.empty());
|
||||
|
||||
mockMvc.perform(get("/api/todos/99"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTodo_ReturnsCreatedTodo() throws Exception {
|
||||
Todo newTodo = new Todo("New Todo", "New Description", false);
|
||||
Todo savedTodo = new Todo("New Todo", "New Description", false);
|
||||
savedTodo.setId(3L);
|
||||
|
||||
when(todoService.createTodo(any(Todo.class))).thenReturn(savedTodo);
|
||||
|
||||
mockMvc.perform(post("/api/todos")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(newTodo)))
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.id", is(3)))
|
||||
.andExpect(jsonPath("$.title", is(newTodo.getTitle())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTodo_WhenExists_ReturnsUpdatedTodo() throws Exception {
|
||||
Todo updatedInfo = new Todo("Updated Title", "Updated Desc", true);
|
||||
Todo updatedTodo = new Todo("Updated Title", "Updated Desc", true);
|
||||
updatedTodo.setId(1L);
|
||||
|
||||
when(todoService.updateTodo(eq(1L), any(Todo.class))).thenReturn(updatedTodo);
|
||||
|
||||
mockMvc.perform(put("/api/todos/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(updatedInfo)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.title", is("Updated Title")))
|
||||
.andExpect(jsonPath("$.completed", is(true)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTodo_WhenNotExists_ReturnsNotFound() throws Exception {
|
||||
Todo updatedInfo = new Todo("Updated Title", "Updated Desc", true);
|
||||
|
||||
when(todoService.updateTodo(eq(99L), any(Todo.class))).thenReturn(null);
|
||||
|
||||
mockMvc.perform(put("/api/todos/99")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(updatedInfo)))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTodo_WhenExists_ReturnsNoContent() throws Exception {
|
||||
when(todoService.deleteTodo(1L)).thenReturn(true);
|
||||
|
||||
mockMvc.perform(delete("/api/todos/1"))
|
||||
.andExpect(status().isNoContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTodo_WhenNotExists_ReturnsNotFound() throws Exception {
|
||||
when(todoService.deleteTodo(99L)).thenReturn(false);
|
||||
|
||||
mockMvc.perform(delete("/api/todos/99"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
6
backend/src/test/resources/application-test.properties
Normal file
6
backend/src/test/resources/application-test.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
Reference in New Issue
Block a user