Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
90e5953
Add HelloController, HelloControllerTest
JamieShin0201 Jan 31, 2021
cdaccf1
Add TaskServiceTest
JamieShin0201 Jan 31, 2021
0ce0971
Add TaskControllerTest
JamieShin0201 Jan 31, 2021
cdd177b
Add HelloControllerWebTest
JamieShin0201 Jan 31, 2021
34a13b4
FIx TaskServiceTest clearly
JamieShin0201 Feb 2, 2021
a9f592e
Add TaskControllerWebTest
JamieShin0201 Feb 2, 2021
008b26e
Add empty tasks test in TaskControllerWebTest
JamieShin0201 Feb 2, 2021
2b6abbd
Add TaskTest
JamieShin0201 Feb 2, 2021
a9f21e1
Add patch test in TestControllerWebTest
JamieShin0201 Feb 2, 2021
6400df4
Add hashCode Test, Add differnt object test about equals
JamieShin0201 Feb 2, 2021
bdfde20
Refactor TaskServiceTest more clearly
JamieShin0201 Feb 2, 2021
9d9e970
Fix TaskControllerWebTest using mockito eq,any method
JamieShin0201 Feb 2, 2021
d66d570
Remove equals, hashcode in Task and TaskTest
JamieShin0201 Feb 2, 2021
597a46d
Fix TaskTest more clearly
JamieShin0201 Feb 4, 2021
ba36cfc
Remove @DisplayName's duplicated description
JamieShin0201 Feb 4, 2021
e0c2bbb
Removed @DisplayName's duplicated description in TaskControllerWebTest
JamieShin0201 Feb 4, 2021
eea9c71
Remove duplicated descriptions
JamieShin0201 Feb 4, 2021
c2d501f
Fix the TaskTest to reveal how to use Task
JamieShin0201 Feb 5, 2021
8bdaa72
Fix 'returns' to 'replies' in TaskControllerWebTest
JamieShin0201 Feb 5, 2021
54569e9
Fix 'replies' to 'responds' in TaskControllerWebTest
JamieShin0201 Feb 5, 2021
88b9435
Simplify method name in TaskControllerWebTest
JamieShin0201 Feb 5, 2021
9cb4b05
Fix method name more clearly in TaskControllerWebTest
JamieShin0201 Feb 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
app/out/
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!";
}

}
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() {
Comment on lines +139 to +140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정된 task를 반환한다고 했는데, 반환한 task를 확인하고 있지 않아요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러네요.. 수정된 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);
}
}
}
}
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!");
}

}
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!")));

}

}
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();
}

}
Loading