-
Notifications
You must be signed in to change notification settings - Fork 89
[Week3] Test Spring Todo REST API #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
90e5953
Add HelloController, HelloControllerTest
JamieShin0201 cdaccf1
Add TaskServiceTest
JamieShin0201 0ce0971
Add TaskControllerTest
JamieShin0201 cdd177b
Add HelloControllerWebTest
JamieShin0201 34a13b4
FIx TaskServiceTest clearly
JamieShin0201 a9f592e
Add TaskControllerWebTest
JamieShin0201 008b26e
Add empty tasks test in TaskControllerWebTest
JamieShin0201 2b6abbd
Add TaskTest
JamieShin0201 a9f21e1
Add patch test in TestControllerWebTest
JamieShin0201 6400df4
Add hashCode Test, Add differnt object test about equals
JamieShin0201 bdfde20
Refactor TaskServiceTest more clearly
JamieShin0201 9d9e970
Fix TaskControllerWebTest using mockito eq,any method
JamieShin0201 d66d570
Remove equals, hashcode in Task and TaskTest
JamieShin0201 597a46d
Fix TaskTest more clearly
JamieShin0201 ba36cfc
Remove @DisplayName's duplicated description
JamieShin0201 e0c2bbb
Removed @DisplayName's duplicated description in TaskControllerWebTest
JamieShin0201 eea9c71
Remove duplicated descriptions
JamieShin0201 c2d501f
Fix the TaskTest to reveal how to use Task
JamieShin0201 8bdaa72
Fix 'returns' to 'replies' in TaskControllerWebTest
JamieShin0201 54569e9
Fix 'replies' to 'responds' in TaskControllerWebTest
JamieShin0201 88b9435
Simplify method name in TaskControllerWebTest
JamieShin0201 9cb4b05
Fix method name more clearly in TaskControllerWebTest
JamieShin0201 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,3 +24,4 @@ Thumbs.db | |
| Thumbs.db:encryptable | ||
| ehthumbs.db | ||
| ehthumbs_vista.db | ||
| app/out/ | ||
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/codesoom/assignment/controllers/HelloController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.codesoom.assignment.controllers; | ||
|
|
||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| public class HelloController { | ||
|
|
||
| @GetMapping | ||
| public String sayHello() { | ||
| return "Hello, world!"; | ||
| } | ||
|
|
||
| } |
196 changes: 196 additions & 0 deletions
196
app/src/test/java/com/codesoom/assignment/application/TaskServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package com.codesoom.assignment.application; | ||
|
|
||
| import com.codesoom.assignment.TaskNotFoundException; | ||
| import com.codesoom.assignment.models.Task; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| @DisplayName("TaskService 클래스") | ||
| class TaskServiceTest { | ||
|
|
||
| private TaskService taskService; | ||
| private Task task; | ||
|
|
||
| private static final String TASK_TITLE = "test"; | ||
| private static final String UPDATE_POSTFIX = "!!!"; | ||
|
|
||
| private static final Long EXISTING_ID = 1L; | ||
| private static final Long NOT_EXISTING_ID = 100L; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| taskService = new TaskService(); | ||
|
|
||
| task = new Task(); | ||
| task.setTitle(TASK_TITLE); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("getTasks") | ||
| class Describe_getTasks { | ||
| @Nested | ||
| @DisplayName("저장된 task가 여러개 있다면") | ||
| class Context_with_tasks { | ||
| @BeforeEach | ||
| void prepareTasks() { | ||
| taskService.createTask(task); | ||
| taskService.createTask(task); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("task list를 리턴한다") | ||
| void it_returns_task_list() { | ||
| assertThat(taskService.getTasks()).hasSize(2); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("저장된 task가 없다면") | ||
| class Context_with_empty_tasks { | ||
| @Test | ||
| @DisplayName("빈 task list를 리턴한다") | ||
| void it_returns_empty_task_list() { | ||
| assertThat(taskService.getTasks()).isEmpty(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("getTask") | ||
| class Describe_getTask { | ||
| @BeforeEach | ||
| void prepareTask() { | ||
| taskService.createTask(task); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("존재하는 task id가 주어진다면") | ||
| class Context_with_an_existing_task_id { | ||
| @Test | ||
| @DisplayName("task를 리턴한다") | ||
| void it_returns_a_task() { | ||
| Task task = taskService.getTask(EXISTING_ID); | ||
|
|
||
| assertThat(task.getTitle()).isEqualTo(TASK_TITLE); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("존재하지 않는 task id가 주어진다면") | ||
| class Context_with_not_an_existing_task_id { | ||
| @Test | ||
| @DisplayName("TaskNotFoundException을 던진다") | ||
| void it_throws_task_not_found_exception() { | ||
| assertThatThrownBy(() -> taskService.getTask(NOT_EXISTING_ID)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("createTask") | ||
| class Describe_createTask { | ||
| int oldSize; | ||
| Task newTask; | ||
|
|
||
| @BeforeEach | ||
| void prepareTask() { | ||
| oldSize = taskService.getTasks().size(); | ||
|
|
||
| newTask = new Task(); | ||
| newTask.setTitle(TASK_TITLE); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("생성된 task를 리턴한다") | ||
| void it_returns_a_created_task() { | ||
| Task createdTask = taskService.createTask(newTask); | ||
|
|
||
| int newSize = taskService.getTasks().size(); | ||
|
|
||
| assertThat(createdTask.getTitle()).isEqualTo(TASK_TITLE); | ||
| assertThat(newSize - oldSize).isEqualTo(1); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("updateTask") | ||
| class Describe_updateTask { | ||
| Task source; | ||
|
|
||
| @BeforeEach | ||
| void prepareTask() { | ||
| taskService.createTask(task); | ||
|
|
||
| source = new Task(); | ||
| source.setTitle(TASK_TITLE + UPDATE_POSTFIX); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("존재하는 task id가 주어진다면") | ||
| class Context_with_an_existing_task_id { | ||
| @Test | ||
| @DisplayName("수정된 task를 리턴한다") | ||
| void it_returns_a_updated_task() { | ||
| Task updatedTask = taskService.updateTask(EXISTING_ID, source); | ||
|
|
||
| assertThat(updatedTask.getTitle()).isEqualTo(TASK_TITLE + UPDATE_POSTFIX); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("존재하지 않는 task id가 주어진다면") | ||
| class Context_with_not_an_existing_task_id { | ||
| @Test | ||
| @DisplayName("TaskNotFoundException을 던진다") | ||
| void it_throws_task_not_found_exception() { | ||
| assertThatThrownBy(() -> taskService.updateTask(NOT_EXISTING_ID, source)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("deleteTask") | ||
| class Describe_deleteTask { | ||
| int oldSize; | ||
|
|
||
| @BeforeEach | ||
| void prepareTask() { | ||
| taskService.createTask(task); | ||
|
|
||
| oldSize = taskService.getTasks().size(); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("존재하는 task id가 주어진다면") | ||
| class Context_with_an_existing_task_id { | ||
| @Test | ||
| @DisplayName("task를 삭제한다") | ||
| void it_deletes_a_task() { | ||
| taskService.deleteTask(EXISTING_ID); | ||
|
|
||
| int newSize = taskService.getTasks().size(); | ||
|
|
||
| assertThat(oldSize - newSize).isEqualTo(1); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("존재하지 않는 task id가 주어진다면") | ||
| class Context_with_not_an_existing_task_id { | ||
| @Test | ||
| @DisplayName("TaskNotFoundException을 던진다") | ||
| void it_throws_task_not_found_exception() { | ||
| assertThatThrownBy(() -> taskService.deleteTask(NOT_EXISTING_ID)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
| } | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
app/src/test/java/com/codesoom/assignment/controllers/HelloControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.codesoom.assignment.controllers; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
|
||
| class HelloControllerTest { | ||
|
|
||
| @Test | ||
| void sayHello() { | ||
| HelloController controller = new HelloController(); | ||
|
|
||
| assertThat(controller.sayHello()).isEqualTo("Hello, world!"); | ||
| } | ||
|
|
||
| } |
29 changes: 29 additions & 0 deletions
29
app/src/test/java/com/codesoom/assignment/controllers/HelloControllerWebTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.codesoom.assignment.controllers; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| 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.test.web.servlet.MockMvc; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| public class HelloControllerWebTest { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @Test | ||
| void sayHello() throws Exception { | ||
| mockMvc.perform(get("/")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().string(containsString("Hello, world!"))); | ||
|
|
||
| } | ||
|
|
||
| } |
109 changes: 109 additions & 0 deletions
109
app/src/test/java/com/codesoom/assignment/controllers/TaskControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| 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 static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class TaskControllerTest { | ||
|
|
||
| private TaskController controller; | ||
| private TaskService taskService; | ||
|
|
||
| private static final String TASK_TITLE = "test"; | ||
| private static final String UPDATE_POSTFIX = "!!!"; | ||
|
|
||
| private static final Long EXISTING_ID = 1L; | ||
| private static final Long NOT_EXISTING_ID = 100L; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| taskService = new TaskService(); | ||
| controller = new TaskController(taskService); | ||
|
|
||
| // fixtures | ||
| Task task = new Task(); | ||
| task.setTitle(TASK_TITLE); | ||
| controller.create(task); | ||
| } | ||
|
|
||
| @Test | ||
| void list() { | ||
| assertThat(controller.list()).hasSize(1); | ||
| } | ||
|
|
||
| @Test | ||
| void detail() { | ||
| Task found = controller.detail(EXISTING_ID); | ||
|
|
||
| assertThat(found.getId()).isEqualTo(EXISTING_ID); | ||
| assertThat(found.getTitle()).isEqualTo(TASK_TITLE); | ||
| } | ||
|
|
||
| @Test | ||
| void detailWithNotExistingId() { | ||
| assertThatThrownBy(() -> controller.detail(NOT_EXISTING_ID)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void create() { | ||
| int oldSize = controller.list().size(); | ||
|
|
||
| Task task = new Task(); | ||
| task.setTitle(TASK_TITLE); | ||
| controller.create(task); | ||
|
|
||
| int newSize = controller.list().size(); | ||
|
|
||
| assertThat(newSize - oldSize).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void update() { | ||
| Task source = new Task(); | ||
| source.setTitle(TASK_TITLE + UPDATE_POSTFIX); | ||
| controller.update(EXISTING_ID, source); | ||
|
|
||
| Task task = controller.detail(EXISTING_ID); | ||
| assertThat(task.getTitle()).isEqualTo(TASK_TITLE + UPDATE_POSTFIX); | ||
| } | ||
|
|
||
| @Test | ||
| void updateWithNotExistingId() { | ||
| Task source = new Task(); | ||
| source.setTitle(TASK_TITLE + UPDATE_POSTFIX); | ||
|
|
||
| assertThatThrownBy(() -> controller.update(NOT_EXISTING_ID, source)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void delete() { | ||
| controller.delete(EXISTING_ID); | ||
|
|
||
| assertThatThrownBy(() -> controller.delete(EXISTING_ID)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void deleteWithNotExistingId() { | ||
| assertThatThrownBy(() -> controller.delete(NOT_EXISTING_ID)) | ||
| .isInstanceOf(TaskNotFoundException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void patch() { | ||
| update(); | ||
| } | ||
|
|
||
| @Test | ||
| void patchWithNotExistId() { | ||
| updateWithNotExistingId(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수정된
task를 반환한다고 했는데, 반환한 task를 확인하고 있지 않아요There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러네요.. 수정된 task를 확인하는 테스트로 수정하였습니다!