-
[Spring Boot] Kotlin으로 REST API 만들기(6) - Service 구현 및 단위 테스트(Junit5)Spring Boot/Kotlin으로 REST API 만들기 2022. 10. 30. 19:56반응형
Kotlin으로 REST API 만들기(6) - Service 구현 및 단위 테스트
TodoService.kt를 구현한 후 JUnit5을 사용하여 단위 테스트를 작성하세요.
1. TodoService.ktpackage com.example.springbootrestapi.service import com.example.springbootrestapi.domain.TodoRequest import com.example.springbootrestapi.domain.TodoResponse import com.example.springbootrestapi.mapper.TodoMapper import org.springframework.stereotype.Service @Service class TodoService( val todoMapper: TodoMapper ) { /** To-Do 조회 */ fun getTodos(todoRequest: TodoRequest): MutableList<TodoResponse> { return todoMapper.getTodos(todoRequest) } /** * To-Do 상세 조회 */ fun getTodoById(id: Long): TodoResponse { return todoMapper.getTodoById(id) } /** * To-Do 저장 */ fun insertTodo(todoRequest: TodoRequest): TodoResponse { var todoResponse = TodoResponse() var result = todoMapper.insertTodo(todoRequest) if(result > 0){ todoRequest.id?.let { todoResponse = todoMapper.getTodoById(it) } } return todoResponse } /** * To-Do 수정 */ fun updateTodo(todoRequest: TodoRequest): TodoResponse { var todoResponse = TodoResponse() var result = todoMapper.updateTodo(todoRequest) if (result > 0) { todoRequest.id?.let { todoResponse = todoMapper.getTodoById(it) } } return todoResponse } /** * To-Do 삭제 */ fun deleteTodoById(id: Long): TodoResponse { var todoResponse = TodoResponse() var result = todoMapper.deleteTodoById(id) if (result > 0) { todoResponse = todoMapper.getTodoById(id) } return todoResponse } }
2. TestTodoService.ktpackage com.example.springbootrestapi.service import com.example.springbootrestapi.domain.TodoRequest import com.example.springbootrestapi.mapper.TodoMapper import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.ActiveProfiles import org.springframework.transaction.annotation.Transactional @SpringBootTest @ActiveProfiles("local") class TodoServiceTest { @Autowired lateinit var todoService: TodoService @Autowired lateinit var todoMapper: TodoMapper @Transactional @DisplayName("getTodos_To-Do 목록 조회") @Test fun 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) val todoRequest = TodoRequest().apply { this.title = "Title Junit Test Insert" this.description = "Description Junit Test Insert" this.completed = true } // When val todoResponses = todoService.getTodos(todoRequest) // Then assertTrue(todoResponses.isNotEmpty()) } @Transactional @DisplayName("getTodoById_To-Do 상세 조회") @Test fun testGetTodoById() { // Given var title = "Title Junit Test Insert" var description = "Description Junit Test Insert" var completed = false var insertId = insertTodo(title, description, completed) // When var todoResponse = todoService.getTodoById(insertId) // Then assertEquals(title, todoResponse.title) assertEquals(description, todoResponse.description) assertEquals(completed, todoResponse.completed) } @Transactional @DisplayName("insertTodo_To-Do 저장") @Test fun testInsertTodo() { // Given val todoRequest = TodoRequest().apply { this.title = "Title Junit Test Insert" this.description = "Description Junit Test Insert" this.completed = true } // When val todoResponse = todoService.insertTodo(todoRequest) // Then assertEquals(todoRequest.title, todoResponse.title) assertEquals(todoRequest.description, todoResponse.description) assertEquals(todoRequest.completed, todoResponse.completed) } @Transactional @DisplayName("updateTodo_To-Do 수정") @Test fun testUpdateTodo() { // Given var insertId = insertTodo("Title Junit Test Insert", "Description Junit Test Insert", false) val todoRequest = TodoRequest().apply { this.id = insertId this.title = "Title Junit Test Update" this.description = "Description Junit Test Upate" this.completed = true } // When val todoResponse = todoService.updateTodo(todoRequest) // Then assertEquals(todoRequest.title, todoResponse.title) assertEquals(todoRequest.description, todoResponse.description) assertEquals(todoRequest.completed, todoResponse.completed) } @Transactional @DisplayName("deleteTodoById_To-Do 삭제") @Test fun testDeleteTodoById() { // Given val insertId = insertTodo("Title Junit Test Insert", "Description Junit Test Insert", false) // When val todoResponse = todoService.deleteTodoById(insertId) // Then assertNull(todoResponse) } fun insertTodo( title: String, description: String, completed: Boolean ): Long { var todoRequest = TodoRequest().apply { this.title = title this.description = description this.completed = completed } todoMapper.insertTodo(todoRequest) return todoRequest.id ?: 0 } }
3. 단위 테스트 확인
좌측에 초록색 버튼(Run Test) 클릭 및 클래스 또는 메소드 내에서 우클릭하여 Run 클릭하여 단위 테스트가 정상적으로 동작하는지 확인하세요. (단축키 : Ctrl + Shift + F10)소스 코드는 Github Repository - https://github.com/tychejin1218/kotlin-springboot-rest-api.git (branch : section0) 를 참조하세요.
GitHub에서 프로젝트 복사하기(Get from Version Control) - https://tychejin.tistory.com/325반응형'Spring Boot > Kotlin으로 REST API 만들기' 카테고리의 다른 글
[Spring Boot] Kotlin으로 REST API 만들기(8) - Interceptor 적용 (0) 2022.11.01 [Spring Boot] Kotlin으로 REST API 만들기(7) - Kotlin으로 Controller 구현 및 단위 테스트(Junit5) (0) 2022.10.30 [Spring Boot] Kotlin으로 REST API 만들기(5) - Mapper 구현 및 단위 테스트(Junit5) (0) 2022.10.30 [Spring Boot] Kotlin으로 REST API 만들기(4) - Log4jdbc 설정 (0) 2022.10.27 [Spring Boot] Kotlin으로 REST API 만들기(3) - Logback 설정 (0) 2022.10.27