-
[Spring Boot] REST API 만들기(6) - Service 구현 및 단위 테스트(Junit5)Spring Boot/2.7.x - REST API 만들기 2022. 10. 3. 22:37반응형
REST API 만들기(6) - Service 구현 및 단위 테스트
TodoService.java를 구현한 후 JUnit5을 사용하여 단위 테스트를 작성하세요.
1. TodoService.javapackage com.example.springbootrestapi.service; import com.example.springbootrestapi.domain.Todo; import com.example.springbootrestapi.mapper.TodoMapper; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @RequiredArgsConstructor @Service public class TodoService { private final TodoMapper todoMapper; /** * To-Do 조회 */ public List<Todo.Response> getTodos(Todo.Request todoRequest) { return todoMapper.getTodos(todoRequest); } /** * To-Do 상세 조회 */ public Todo.Response getTodoById(int id) { return todoMapper.getTodoById(id); } /** * To-Do 저장 */ public Todo.Response insertTodo(Todo.Request todoRequest) { Todo.Response todoResponse = Todo.Response.builder().build(); todoMapper.insertTodo(todoRequest); if (!ObjectUtils.isEmpty(todoRequest.getId())) { todoResponse = todoMapper.getTodoById(todoRequest.getId()); } return todoResponse; } /** * To-Do 수정 */ public Todo.Response updateTodo(Todo.Request todoRequest) { Todo.Response todoResponse = Todo.Response.builder().build(); todoMapper.updateTodo(todoRequest); if (!ObjectUtils.isEmpty(todoRequest.getId())) { todoResponse = todoMapper.getTodoById(todoRequest.getId()); } return todoResponse; } /** * To-Do 삭제 */ public int deleteTodoById(int id) { return todoMapper.deleteTodoById(id); } }
2. TestTodoService.javapackage com.example.springbootrestapi.service; import com.example.springbootrestapi.domain.Todo; import com.example.springbootrestapi.domain.Todo.Response; import com.example.springbootrestapi.mapper.TodoMapper; import java.util.List; import javax.annotation.Resource; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.transaction.annotation.Transactional; @SpringBootTest @ActiveProfiles("local") class TodoServiceTest { @Resource TodoService todoService; @Resource TodoMapper todoMapper; @Transactional @DisplayName("getTodos_To-Do 목록 조회") @Test void testGetTodos() { // Given insertTodo("Title Junit Test Insert 01", "Description Junit Test Insert 01", false); insertTodo("Title Junit Test Insert 02", "Description Junit Test Insert 02", true); insertTodo("Title Junit Test Insert 03", "Description Junit Test Insert 03", false); insertTodo("Title Junit Test Insert 04", "Description Junit Test Insert 04", true); insertTodo("Title Junit Test Insert 05", "Description Junit Test Insert 05", false); Todo.Request todoRequest = Todo.Request.builder() .title("Title Junit Test Insert") .description("Description Junit Test Insert") .completed(true) .build(); // When List<Response> todoResponses = todoService.getTodos(todoRequest); // Then Assertions.assertTrue(!todoResponses.isEmpty()); } @Transactional @DisplayName("getTodoById_To-Do 상세 조회") @Test void testGetTodoById() { // Given String title = "Title Junit Test Insert"; String description = "Description Junit Test Insert"; boolean completed = false; int insertId = insertTodo(title, description, completed); // When Todo.Response todoResponse = todoService.getTodoById(insertId); // Then Assertions.assertEquals(title, todoResponse.getTitle()); Assertions.assertEquals(description, todoResponse.getDescription()); Assertions.assertEquals(completed, todoResponse.getCompleted()); } @Transactional @DisplayName("insertTodo_To-Do 저장") @Test void testInsertTodo() { // Given Todo.Request todoRequest = Todo.Request.builder() .title("Title Junit Test Insert") .description("Description Junit Test Insert") .completed(false) .build(); // When Todo.Response todoResponse = todoService.insertTodo(todoRequest); // Then Assertions.assertEquals(todoRequest.getTitle(), todoResponse.getTitle()); Assertions.assertEquals(todoRequest.getDescription(), todoResponse.getDescription()); Assertions.assertEquals(todoRequest.getCompleted(), todoResponse.getCompleted()); } @Transactional @DisplayName("updateTodo_To-Do 수정") @Test void testUpdateTodo() { // Given String title = "Title Junit Test Insert"; String description = "Description Junit Test Insert"; boolean completed = false; int insertId = insertTodo(title, description, completed); Todo.Request todoRequest = Todo.Request.builder() .id(insertId) .title("Title Junit Test Update") .description("Description Junit Test Update") .completed(true) .build(); // When Todo.Response todoResponse = todoService.updateTodo(todoRequest); // Then Assertions.assertEquals(todoRequest.getTitle(), todoResponse.getTitle()); Assertions.assertEquals(todoRequest.getDescription(), todoResponse.getDescription()); Assertions.assertEquals(todoRequest.getCompleted(), todoResponse.getCompleted()); } @Transactional @DisplayName("deleteTodoById_To-Do 삭제") @Test void testDeleteTodoById() { // Given int insertId = insertTodo("Title Junit Test Insert", "Description Junit Test Insert", false); // When todoService.deleteTodoById(insertId); // Then Todo.Response todoResponse = todoService.getTodoById(insertId); Assertions.assertNull(todoResponse); } /** * To-Do 저장 */ int insertTodo( String title, String description, Boolean completed) { Todo.Request todoRequest = Todo.Request.builder() .title(title) .description(description) .completed(completed) .build(); todoMapper.insertTodo(todoRequest); return todoRequest.getId(); } }
3. 단위 테스트 확인
좌측에 초록색 버튼(Run Test)을 클릭 또는 소스 코드에서 우클릭하여 Run을 클릭하여 단위 테스트가 정상적으로 동작하는지 확인하세요. (단축키 : Ctrl + Shift + F10)소스 코드는 Github Repository - https://github.com/tychejin1218/springboot-rest-api (branch : section06) 를 참조하세요.
GitHub에서 프로젝트 복사하기(Get from Version Control) - https://tychejin.tistory.com/325반응형'Spring Boot > 2.7.x - REST API 만들기' 카테고리의 다른 글
[Spring Boot] REST API 만들기(8) - Interceptor 적용 (0) 2022.10.07 [Spring Boot] REST API 만들기(7) - Controller 구현 및 단위 테스트(Junit5) (0) 2022.10.05 [Spring Boot] REST API 만들기(5) - Mapper 구현 및 단위 테스트(Junit5) (0) 2022.09.25 [Spring Boot] REST API 만들기(4) - Log4jdbc 설정 (0) 2022.09.15 [Spring Boot] REST API 만들기(3) - Logback 설정 (0) 2022.09.12