From b8f561523e4a07375a54ba803e12296791336d05 Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Wed, 19 Jul 2023 05:11:27 +0900 Subject: [PATCH 1/9] =?UTF-8?q?3=EC=A3=BC=EC=B0=A8=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TaskServiceTest.java | 48 ++++++++ .../controllers/TaskControllerTest.java | 107 ++++++++++++++++++ .../controllers/TaskControllerWebTest.java | 71 ++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java create mode 100644 app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java create mode 100644 app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java diff --git a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java new file mode 100644 index 00000000..707f9c23 --- /dev/null +++ b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java @@ -0,0 +1,48 @@ +package com.codesoom.assignment.application; + +import com.codesoom.assignment.TaskNotFoundException; +import com.codesoom.assignment.models.Task; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +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.boot.test.mock.mockito.MockBean; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.BDDMockito.given; + +@SpringBootTest +class TaskServiceTest { + @Autowired + TaskService taskService; + + @BeforeEach + void setUp() { + for (int i = 0; i < 10; i++) { + Task task = new Task(); + task.setTitle("task" + i); + taskService.createTask(task); + } + } + + @Test + @DisplayName("할일 조회 리스트 성공 테스트") + void getTasksSuccess() { + Assertions.assertThat(taskService.getTasks()).hasSize(10); + } + + @Test + @DisplayName("할일 상세 조회 성공 테스트") + void getTaskSuccess(){ + Assertions.assertThat(taskService.getTask(1L).getTitle()).isEqualTo("task0"); + } + + @Test + @DisplayName("할일 상세 조회시 할일이 없는 경우") + void getTaskFail(){ + Assertions.assertThatThrownBy(() -> taskService.getTask(100L)).isInstanceOf(TaskNotFoundException.class); + } + +} diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java new file mode 100644 index 00000000..29082e4b --- /dev/null +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java @@ -0,0 +1,107 @@ +package com.codesoom.assignment.controllers; + +import com.codesoom.assignment.TaskNotFoundException; +import com.codesoom.assignment.application.TaskService; +import com.codesoom.assignment.models.Task; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; + + +class TaskControllerTest { + private TaskController taskController; + + @BeforeEach + void setUp() { + taskController = new TaskController(new TaskService()); + } + + public Task createTask() { + Task task = new Task(); + task.setTitle("task"); + return task; + } + + private void createTasks() { + for (int i = 1; i <= 10; i++) { + Task task = new Task(); + task.setTitle("task" + i); + taskController.create(task); + } + } + + @Test + @DisplayName("할일 생성 성공 테스트.") + void createTaskSuccessTest() { + Task task = createTask(); + taskController.create(task); + Assertions.assertThat(taskController.list()).hasSize(1); + } + + @Test + @DisplayName("최초 할일 리스트가 비어있는지 테스트.") + void isFirstTasksIsEmpty() { + Assertions.assertThat(taskController.list()).isEmpty(); + } + + @Test + @DisplayName("할일 리스트가 정상적으로 나오는 지 테스트한다.") + void listSuccessTest() { + createTasks(); + Assertions.assertThat(taskController.list()).hasSize(10); + } + + + @Test + @DisplayName("할일 상세 조회 성공 테스트") + void getTaskSuccess() { + createTasks(); + Assertions.assertThat(taskController.detail(4L).getTitle()).isEqualTo("task4"); + } + + @Test + @DisplayName("할일 상세 조회 실패 테스트") + void getTaskFail() { + createTasks(); + Assertions.assertThatThrownBy(() -> taskController.detail(100L)) + .isInstanceOf(TaskNotFoundException.class); + } + + @Test + @DisplayName("할일 수정 성공 테스트") + void updateTaskTest() { + Task task = taskController.create(createTask()); + Task updateTask = new Task(); + updateTask.setTitle("updated"); + taskController.update(task.getId(), updateTask); + Assertions.assertThat(taskController.detail(task.getId()).getTitle()).isEqualTo("updated"); + } + + @Test + @DisplayName("할일 수정 실패 테스트") + void updatedFailTest() { + Task task = taskController.create(createTask()); + Task updateTask = new Task(); + updateTask.setTitle("updated"); + taskController.update(task.getId(), updateTask); + Assertions.assertThatThrownBy(() -> taskController.update(100L, updateTask)) + .isInstanceOf(TaskNotFoundException.class); + } + + @Test + @DisplayName("할일 삭제 성공 테스트") + void deleteTaskSuccess() { + taskController.create(createTask()); + taskController.delete(1L); + Assertions.assertThat(taskController.list()).isEmpty(); + } + + @Test + @DisplayName("할일 삭제 실패 테스트") + void deleteTaskFail() { + Assertions.assertThatThrownBy(() -> taskController.delete(1L)).isInstanceOf(TaskNotFoundException.class); + } +} diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java new file mode 100644 index 00000000..b61a4d87 --- /dev/null +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java @@ -0,0 +1,71 @@ +package com.codesoom.assignment.controllers; + +import com.codesoom.assignment.TaskNotFoundException; +import com.codesoom.assignment.application.TaskService; +import com.codesoom.assignment.models.Task; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.BDDMockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.test.web.client.match.MockRestRequestMatchers; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import static org.hamcrest.Matchers.containsString; + +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.BDDMockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +class TaskControllerWebTest { + @Autowired + private MockMvc mockMvc; + + @MockBean + private TaskService taskService; + + @BeforeEach + void setUp() { + List tasks = new ArrayList<>(); + Task task = new Task(); + task.setTitle("test task"); + tasks.add(task); + + given(taskService.getTasks()).willReturn(tasks); + + given(taskService.getTask(1L)).willReturn(task); + + given(taskService.getTask(100L)) + .willThrow(new TaskNotFoundException(100L)); + } + @Test + void list() throws Exception { + mockMvc.perform(get("/tasks")) + .andExpect((status().isOk())) + .andExpect(content().string(containsString("test task"))); + } + + @Test + void detailWithValidId() throws Exception { + mockMvc.perform(get("/tasks/1")) + .andExpect(status().isOk()); + } + + @Test + void detailWithInvalidId() throws Exception { + mockMvc.perform(get("/tasks/100")) + .andExpect(status().isNotFound()); + } +} From 4f661a9ad00e438fea1445e3c390a902c84a7287 Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Wed, 19 Jul 2023 21:13:32 +0900 Subject: [PATCH 2/9] =?UTF-8?q?test=20:=20TaskService=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TaskServiceTest.java | 115 ++++++++++++++++-- 1 file changed, 102 insertions(+), 13 deletions(-) diff --git a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java index 707f9c23..bebc9205 100644 --- a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java +++ b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java @@ -15,34 +15,123 @@ @SpringBootTest class TaskServiceTest { - @Autowired + TaskService taskService; @BeforeEach void setUp() { - for (int i = 0; i < 10; i++) { - Task task = new Task(); - task.setTitle("task" + i); - taskService.createTask(task); - } + taskService = new TaskService(); + } + @Test + @DisplayName("최초 할일 리스트 요청시 빈 리스트 반환 테스트") + void getFirstTasksIsEmpty() { + Assertions.assertThat(taskService.getTasks()).hasSize(0); } @Test - @DisplayName("할일 조회 리스트 성공 테스트") + @DisplayName("기존에 할일이 2개 있는 경우 2개 조회 테스트") void getTasksSuccess() { - Assertions.assertThat(taskService.getTasks()).hasSize(10); + // given + Task task1 = new Task(); + Task task2 = new Task(); + + taskService.createTask(task1); + taskService.createTask(task2); + + // when & then + Assertions.assertThat(taskService.getTasks()).hasSize(2); } @Test - @DisplayName("할일 상세 조회 성공 테스트") - void getTaskSuccess(){ + @DisplayName("할일이 존재하는 경우 상세 조회 테스트 ") + void getTaskSuccess() { + // given + Task task = new Task(); + task.setTitle("task0"); + taskService.createTask(task); + + // when & then Assertions.assertThat(taskService.getTask(1L).getTitle()).isEqualTo("task0"); } @Test - @DisplayName("할일 상세 조회시 할일이 없는 경우") - void getTaskFail(){ + @DisplayName("할일 상세 조회시 할일이 없는 경우 테스트") + void getTaskFail() { + // when & then Assertions.assertThatThrownBy(() -> taskService.getTask(100L)).isInstanceOf(TaskNotFoundException.class); } - + + @Test + @DisplayName("할일을 생성후 할일이 생성되었는지 확인하는 테스트") + void createTaskSuccess() { + // given + Task task = new Task(); + task.setTitle("task0"); + + // when + Task createdTask = taskService.createTask(task); + + // then + Assertions.assertThat(createdTask.getTitle()).isEqualTo("task0"); + } + + @Test + @DisplayName("할일을 수정후 수정된 할일이 반환되는지 확인하는 테스트") + void updateTaskSuccess() { + // given + Task task = new Task(); + task.setTitle("task0"); + taskService.createTask(task); + + Task updateTask = new Task(); + updateTask.setTitle("task1"); + + // when + Task updatedTask = taskService.updateTask(1L, updateTask); + + // then + Assertions.assertThat(updatedTask.getTitle()).isEqualTo("task1"); + } + @Test + @DisplayName("존재하지 않는 할일을 업데이트 할 경우 예외를 반환하는 테스트") + void notExistTaskUpdateFail() { + // given + Task task = new Task(); + task.setTitle("task0"); + taskService.createTask(task); + + Task updateTask = new Task(); + updateTask.setTitle("task1"); + + // when & then + Assertions.assertThatThrownBy(() -> taskService.updateTask(100L, updateTask)).isInstanceOf(TaskNotFoundException.class); + } + + @Test + @DisplayName("할일을 삭제 후 삭제 되었는지 확인하는 테스트") + void deleteTaskSuccess() { + // given + Task task = new Task(); + task.setTitle("task0"); + taskService.createTask(task); + + // when + taskService.deleteTask(1L); + + // then + Assertions.assertThat(taskService.getTasks()).hasSize(0); + } + + @Test + @DisplayName("존재하지 않는 할일을 삭제 할 경우 예외를 반환하는 테스트") + void isNotExistTaskDeleteFail() { + // given + Task task = new Task(); + task.setTitle("task0"); + taskService.createTask(task); + + // when & then + Assertions.assertThatThrownBy(() -> taskService.deleteTask(100L)).isInstanceOf(TaskNotFoundException.class); + } + } From 6da3a4408e0bf3d6de9588de0f61b44ac432586a Mon Sep 17 00:00:00 2001 From: "DESKTOP-ES74E7O\\user" Date: Thu, 20 Jul 2023 08:44:21 +0900 Subject: [PATCH 3/9] =?UTF-8?q?test=20:=20TaskControllerTest=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/TaskControllerTest.java | 88 +++++++++++++------ 1 file changed, 62 insertions(+), 26 deletions(-) diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java index 29082e4b..b28c7486 100644 --- a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java @@ -25,82 +25,118 @@ public Task createTask() { return task; } - private void createTasks() { - for (int i = 1; i <= 10; i++) { - Task task = new Task(); - task.setTitle("task" + i); - taskController.create(task); - } - } - @Test - @DisplayName("할일 생성 성공 테스트.") + @DisplayName("최초 할일 생성 후 할일 리스트에 값이 늘어난지 테스트") void createTaskSuccessTest() { + // given Task task = createTask(); - taskController.create(task); - Assertions.assertThat(taskController.list()).hasSize(1); + int beforeSize = taskController.list().size(); + // when + Task insertTask = taskController.create(task); + + // then + Assertions.assertThat( + beforeSize).isEqualTo(taskController.list().size() - 1); + } @Test - @DisplayName("최초 할일 리스트가 비어있는지 테스트.") + @DisplayName("최초 호출시 할일 리스트가 비어있는지 테스트.") void isFirstTasksIsEmpty() { Assertions.assertThat(taskController.list()).isEmpty(); } @Test - @DisplayName("할일 리스트가 정상적으로 나오는 지 테스트한다.") + @DisplayName("할일 복수 있는 경우 해당 개수에 맞게 나오는지 테스트.") void listSuccessTest() { - createTasks(); - Assertions.assertThat(taskController.list()).hasSize(10); + // given + Task task1 = createTask(); + Task task2 = createTask(); + + // when + taskController.create(task1); + taskController.create(task2); + + // then + Assertions.assertThat(taskController.list()).hasSize(2); } @Test - @DisplayName("할일 상세 조회 성공 테스트") + @DisplayName("할일을 1개를 등록 후 해당 할일 상세 조회 테스트") void getTaskSuccess() { - createTasks(); - Assertions.assertThat(taskController.detail(4L).getTitle()).isEqualTo("task4"); + // given + Task task1 = createTask(); + + // when + taskController.create(task1); + + // then + Assertions.assertThat(taskController.detail(1L).getTitle()).isEqualTo("task1"); + } @Test - @DisplayName("할일 상세 조회 실패 테스트") + @DisplayName("존재하지 않지 않는 할일 조회시 TaskNotFound 예외 발생 테스트") void getTaskFail() { - createTasks(); Assertions.assertThatThrownBy(() -> taskController.detail(100L)) .isInstanceOf(TaskNotFoundException.class); } @Test - @DisplayName("할일 수정 성공 테스트") + @DisplayName("할일 수정 후 해당 할일 값이 수정되었는지 테스트") void updateTaskTest() { + // given Task task = taskController.create(createTask()); Task updateTask = new Task(); updateTask.setTitle("updated"); + + // when taskController.update(task.getId(), updateTask); + + // then Assertions.assertThat(taskController.detail(task.getId()).getTitle()).isEqualTo("updated"); } @Test - @DisplayName("할일 수정 실패 테스트") + @DisplayName("존재하지 않는 할일을 수정할 경우 TaskNotFound 예외 발생 테스트") void updatedFailTest() { + // given Task task = taskController.create(createTask()); Task updateTask = new Task(); updateTask.setTitle("updated"); taskController.update(task.getId(), updateTask); + // when then Assertions.assertThatThrownBy(() -> taskController.update(100L, updateTask)) .isInstanceOf(TaskNotFoundException.class); } @Test - @DisplayName("할일 삭제 성공 테스트") + @DisplayName("1개 할일을 삭제 후 해당 리스트의 삭제후 리스트가 비어있는지 테스트") void deleteTaskSuccess() { - taskController.create(createTask()); - taskController.delete(1L); + // given + Task task = taskController.create(createTask()); + // when + taskController.delete(task.getId()); + // then Assertions.assertThat(taskController.list()).isEmpty(); } @Test - @DisplayName("할일 삭제 실패 테스트") + @DisplayName("2개 할일을 삭제 후 해당 리스트의 삭제후 리스트크기가 감소했는 지 테스트") + void deleteTasksSuccess() { + // given + taskController.create(createTask()); + Task task = taskController.create(createTask()); + int beforeSize = taskController.list().size(); + // when + taskController.delete(task.getId()); + // then + Assertions.assertThat(taskController.list().size()).isEqualTo(beforeSize - 1); + } + + @Test + @DisplayName("존재하지 않는 할일을 삭제할 경우 TaskNotFound 예외 발생 테스트") void deleteTaskFail() { Assertions.assertThatThrownBy(() -> taskController.delete(1L)).isInstanceOf(TaskNotFoundException.class); } From e1146dcbafa02d95e4d433a6188f3a71cf1c7914 Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Fri, 21 Jul 2023 03:34:05 +0900 Subject: [PATCH 4/9] =?UTF-8?q?test=20:=20=ED=94=BC=EB=93=9C=EB=B0=B1=20?= =?UTF-8?q?=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TaskServiceTest.java | 29 +++++++++++-------- .../controllers/TaskControllerTest.java | 22 +++++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java index bebc9205..2d2f94b1 100644 --- a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java +++ b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java @@ -10,6 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.given; @@ -23,9 +24,9 @@ void setUp() { taskService = new TaskService(); } @Test - @DisplayName("최초 할일 리스트 요청시 빈 리스트 반환 테스트") + @DisplayName("getTasks는 할 일이 비어있으면 빈 리스트를 반환한다.") void getFirstTasksIsEmpty() { - Assertions.assertThat(taskService.getTasks()).hasSize(0); + assertThat(taskService.getTasks()).hasSize(0); } @Test @@ -39,11 +40,11 @@ void getTasksSuccess() { taskService.createTask(task2); // when & then - Assertions.assertThat(taskService.getTasks()).hasSize(2); + assertThat(taskService.getTasks()).hasSize(2); } @Test - @DisplayName("할일이 존재하는 경우 상세 조회 테스트 ") + @DisplayName("getTasks는 해당하는 할일이 존재하는 경우 상세 할일 내용을 반환한다") void getTaskSuccess() { // given Task task = new Task(); @@ -51,14 +52,14 @@ void getTaskSuccess() { taskService.createTask(task); // when & then - Assertions.assertThat(taskService.getTask(1L).getTitle()).isEqualTo("task0"); + assertThat(taskService.getTask(1L).getTitle()).isEqualTo("task0"); } @Test - @DisplayName("할일 상세 조회시 할일이 없는 경우 테스트") + @DisplayName("getTasks는 할일 상세 조회시 할일이 없는 경우 TaskNotFound 예외를 반환한다") void getTaskFail() { // when & then - Assertions.assertThatThrownBy(() -> taskService.getTask(100L)).isInstanceOf(TaskNotFoundException.class); + assertThatThrownBy(() -> taskService.getTask(100L)).isInstanceOf(TaskNotFoundException.class); } @Test @@ -72,7 +73,7 @@ void createTaskSuccess() { Task createdTask = taskService.createTask(task); // then - Assertions.assertThat(createdTask.getTitle()).isEqualTo("task0"); + assertThat(createdTask.getTitle()).isEqualTo("task0"); } @Test @@ -90,7 +91,7 @@ void updateTaskSuccess() { Task updatedTask = taskService.updateTask(1L, updateTask); // then - Assertions.assertThat(updatedTask.getTitle()).isEqualTo("task1"); + assertThat(updatedTask.getTitle()).isEqualTo("task1"); } @Test @DisplayName("존재하지 않는 할일을 업데이트 할 경우 예외를 반환하는 테스트") @@ -104,7 +105,7 @@ void notExistTaskUpdateFail() { updateTask.setTitle("task1"); // when & then - Assertions.assertThatThrownBy(() -> taskService.updateTask(100L, updateTask)).isInstanceOf(TaskNotFoundException.class); + assertThatThrownBy(() -> taskService.updateTask(100L, updateTask)).isInstanceOf(TaskNotFoundException.class); } @Test @@ -115,11 +116,15 @@ void deleteTaskSuccess() { task.setTitle("task0"); taskService.createTask(task); + int oldSize = taskService.getTasks().size(); + // when taskService.deleteTask(1L); + int newSize = taskService.getTasks().size(); + // then - Assertions.assertThat(taskService.getTasks()).hasSize(0); + assertThat(newSize - oldSize).isEqualTo(-1); } @Test @@ -131,7 +136,7 @@ void isNotExistTaskDeleteFail() { taskService.createTask(task); // when & then - Assertions.assertThatThrownBy(() -> taskService.deleteTask(100L)).isInstanceOf(TaskNotFoundException.class); + assertThatThrownBy(() -> taskService.deleteTask(100L)).isInstanceOf(TaskNotFoundException.class); } } diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java index b28c7486..9a054ef1 100644 --- a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java @@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import static org.assertj.core.api.Assertions.*; + class TaskControllerTest { private TaskController taskController; @@ -35,7 +37,7 @@ void createTaskSuccessTest() { Task insertTask = taskController.create(task); // then - Assertions.assertThat( + assertThat( beforeSize).isEqualTo(taskController.list().size() - 1); } @@ -43,7 +45,7 @@ void createTaskSuccessTest() { @Test @DisplayName("최초 호출시 할일 리스트가 비어있는지 테스트.") void isFirstTasksIsEmpty() { - Assertions.assertThat(taskController.list()).isEmpty(); + assertThat(taskController.list()).isEmpty(); } @Test @@ -58,7 +60,7 @@ void listSuccessTest() { taskController.create(task2); // then - Assertions.assertThat(taskController.list()).hasSize(2); + assertThat(taskController.list()).hasSize(2); } @@ -72,14 +74,14 @@ void getTaskSuccess() { taskController.create(task1); // then - Assertions.assertThat(taskController.detail(1L).getTitle()).isEqualTo("task1"); + assertThat(taskController.detail(1L).getTitle()).isEqualTo("task1"); } @Test @DisplayName("존재하지 않지 않는 할일 조회시 TaskNotFound 예외 발생 테스트") void getTaskFail() { - Assertions.assertThatThrownBy(() -> taskController.detail(100L)) + assertThatThrownBy(() -> taskController.detail(100L)) .isInstanceOf(TaskNotFoundException.class); } @@ -95,7 +97,7 @@ void updateTaskTest() { taskController.update(task.getId(), updateTask); // then - Assertions.assertThat(taskController.detail(task.getId()).getTitle()).isEqualTo("updated"); + assertThat(taskController.detail(task.getId()).getTitle()).isEqualTo("updated"); } @Test @@ -107,7 +109,7 @@ void updatedFailTest() { updateTask.setTitle("updated"); taskController.update(task.getId(), updateTask); // when then - Assertions.assertThatThrownBy(() -> taskController.update(100L, updateTask)) + assertThatThrownBy(() -> taskController.update(100L, updateTask)) .isInstanceOf(TaskNotFoundException.class); } @@ -119,7 +121,7 @@ void deleteTaskSuccess() { // when taskController.delete(task.getId()); // then - Assertions.assertThat(taskController.list()).isEmpty(); + assertThat(taskController.list()).isEmpty(); } @Test @@ -132,12 +134,12 @@ void deleteTasksSuccess() { // when taskController.delete(task.getId()); // then - Assertions.assertThat(taskController.list().size()).isEqualTo(beforeSize - 1); + assertThat(taskController.list().size()).isEqualTo(beforeSize - 1); } @Test @DisplayName("존재하지 않는 할일을 삭제할 경우 TaskNotFound 예외 발생 테스트") void deleteTaskFail() { - Assertions.assertThatThrownBy(() -> taskController.delete(1L)).isInstanceOf(TaskNotFoundException.class); + assertThatThrownBy(() -> taskController.delete(1L)).isInstanceOf(TaskNotFoundException.class); } } From e468b6729a576ff133db9419a31d2083d8f617ca Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Fri, 21 Jul 2023 04:41:47 +0900 Subject: [PATCH 5/9] =?UTF-8?q?test=20:=20TaskControllerWebTest=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EC=82=AD=EC=A0=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/TaskControllerWebTest.java | 70 ++++++++++++++++--- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java index b61a4d87..c3ca3cac 100644 --- a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerWebTest.java @@ -4,6 +4,7 @@ import com.codesoom.assignment.application.TaskService; import com.codesoom.assignment.models.Task; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.BDDMockito; import org.springframework.beans.factory.annotation.Autowired; @@ -36,36 +37,85 @@ class TaskControllerWebTest { @MockBean private TaskService taskService; - @BeforeEach - void setUp() { + @Test + @DisplayName("요청한 할일목록 있는 경우 해당할일 응답") + void list() throws Exception { + //given List tasks = new ArrayList<>(); Task task = new Task(); task.setTitle("test task"); tasks.add(task); - given(taskService.getTasks()).willReturn(tasks); - given(taskService.getTask(1L)).willReturn(task); - - given(taskService.getTask(100L)) - .willThrow(new TaskNotFoundException(100L)); - } - @Test - void list() throws Exception { + // expect mockMvc.perform(get("/tasks")) .andExpect((status().isOk())) .andExpect(content().string(containsString("test task"))); } @Test + @DisplayName("요청한 할일이 있는 경우 200 상태코드와 조회한 할일 응답") void detailWithValidId() throws Exception { + // given + Task task = new Task(); + given(taskService.getTask(1L)).willReturn(task); + + // expect mockMvc.perform(get("/tasks/1")) .andExpect(status().isOk()); } @Test + @DisplayName("요청한 할일이 없는 경우 404 상태코드 응답") void detailWithInvalidId() throws Exception { + // given + given(taskService.getTask(100L)) + .willThrow(new TaskNotFoundException(100L)); + // expect mockMvc.perform(get("/tasks/100")) .andExpect(status().isNotFound()); } + + @Test + @DisplayName("요청한 할일 생성시 201 상태코드 응답 후 생성된 할일 응답") + void createTask() throws Exception { + // given + Task task = new Task(); + task.setTitle("test task"); + given(taskService.createTask(any(Task.class))).willReturn(task); + + // expect + mockMvc.perform(post("/tasks") + .contentType("application/json") + .content("{\"title\":\"test task\"}")) + .andExpect(status().isCreated()) + .andExpect(content().string(containsString("test task"))); + } + + @Test + @DisplayName("요청한 할일 수정시 200 상태코드 응답 후 수정된 할일 응답") + void updateTask() throws Exception { + // given + Task task = new Task(); + task.setTitle("test task2"); + given(taskService.updateTask(any(Long.class), any(Task.class))).willReturn(task); + + // expect + mockMvc.perform(put("/tasks/1") + .contentType("application/json") + .content("{\"title\":\"test task2\"}")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("test task2"))); + } + + @Test + @DisplayName("요청한 할일 수정시 203 상태코드 응답") + void deleteTask() throws Exception { + // given + given(taskService.deleteTask(any(Long.class))).willReturn(null); + + // expect + mockMvc.perform(delete("/tasks/1")) + .andExpect(status().isNoContent()); + } } From 04673fdac3b5ac958c5b2a6a0fa451a8a9b6fe09 Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Sun, 23 Jul 2023 06:50:13 +0900 Subject: [PATCH 6/9] =?UTF-8?q?test=20:=20@SpringBootTest=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/codesoom/assignment/application/TaskServiceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java index 2d2f94b1..d19de699 100644 --- a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java +++ b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.DisplayName; 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.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -14,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.given; -@SpringBootTest + class TaskServiceTest { TaskService taskService; From a0c183d8b2f93875cbb24d66110e7f46f6c1ccae Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Sun, 23 Jul 2023 07:20:08 +0900 Subject: [PATCH 7/9] =?UTF-8?q?test=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20insertTask=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/codesoom/assignment/controllers/TaskControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java index 9a054ef1..dd9a5894 100644 --- a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java @@ -34,7 +34,7 @@ void createTaskSuccessTest() { Task task = createTask(); int beforeSize = taskController.list().size(); // when - Task insertTask = taskController.create(task); + taskController.create(task); // then assertThat( From a645d9a107f7151ca47bbf40ee3569b223e9b12f Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Sun, 23 Jul 2023 06:50:13 +0900 Subject: [PATCH 8/9] =?UTF-8?q?test=20:=20`@SpringBootTest`=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/codesoom/assignment/application/TaskServiceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java index 2d2f94b1..d19de699 100644 --- a/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java +++ b/app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.DisplayName; 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.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -14,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.given; -@SpringBootTest + class TaskServiceTest { TaskService taskService; From 0586f34eeb5357287a0cb9221fcdfa9b5b8cb21f Mon Sep 17 00:00:00 2001 From: tmxhsk99 Date: Sun, 23 Jul 2023 07:20:08 +0900 Subject: [PATCH 9/9] =?UTF-8?q?test=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20insertTask=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/codesoom/assignment/controllers/TaskControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java index 9a054ef1..dd9a5894 100644 --- a/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java +++ b/app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java @@ -34,7 +34,7 @@ void createTaskSuccessTest() { Task task = createTask(); int beforeSize = taskController.list().size(); // when - Task insertTask = taskController.create(task); + taskController.create(task); // then assertThat(