Skip to content
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

ST-2 create_성공_후_NotiManager_호출 #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions src/main/java/net/slipp/notification/NotiManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.slipp.notification;

public class NotiManager {
public void notify(String title) {}
}
5 changes: 5 additions & 0 deletions src/main/java/net/slipp/todo_list/TodoManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.slipp.todo_list;

import net.slipp.exception.RepositoryFailedException;
import net.slipp.notification.NotiManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -17,6 +18,9 @@ public class TodoManager {
@Autowired
private TodoRepository todoRepository;

@Autowired
private NotiManager notiManager;


public void create(Todo todo) {

Expand All @@ -26,6 +30,7 @@ public void create(Todo todo) {

try {
todoRepository.store(todo);
notiManager.notify(todo.getTitle());
} catch (RepositoryFailedException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.slipp.todo_list;

import net.slipp.exception.RepositoryFailedException;
import net.slipp.notification.NotiManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -16,13 +17,16 @@


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class, TodoManagerTest.MockBeanConfig.class } )
public class TodoManagerTest {
@ContextConfiguration(classes = { AppConfig.class, TodoManagerCreateTest.MockBeanConfig.class } )
public class TodoManagerCreateTest {

@Before
public void setUp() throws Exception {
MockTodoRepository.passedTodo = null;
MockTodoRepository.exception = null;

MockNotiManager.passedTitle = null;
MockNotiManager.exception = null;
}

@After
Expand All @@ -43,6 +47,12 @@ public static class MockBeanConfig {
public TodoRepository todoRepository() {
return new MockTodoRepository();
}

@Bean
@Primary
public NotiManager notiManager() {
return new MockNotiManager();
}
}


Expand Down Expand Up @@ -228,6 +238,58 @@ public TodoRepository todoRepository() {
todoManager.create(todo);
}

@Test
public void store_성공_후_NotiManager_호출_확인() {
String TITLE = "TITLE";
String CONTENT = "CONTENT";

Todo todo = new Todo();
todo.setTitle(TITLE);
todo.setContent(CONTENT);

todoManager.create(todo);

String notifyTitle = MockNotiManager.passedTitle;

assertEquals(TITLE, notifyTitle);
}

@Test
public void store_실패_후_NotiManager_미호출_확인() {
String TITLE = "TITLE";
String CONTENT = "CONTENT";

Todo todo = new Todo();
todo.setTitle(TITLE);
todo.setContent(CONTENT);

MockTodoRepository.exception = new RuntimeException();

try {
todoManager.create(todo);
} catch (Exception e) {
// ignore
}

String notifyTitle = MockNotiManager.passedTitle;

assertNull(notifyTitle);
}

@Test(expected = RuntimeException.class)
public void NotiManager_notify_호출_시에_RuntimeException을_던지면_그대로_RuntimeException_던지는_지_확인 () {
String TITLE = "TITLE";
String CONTENT = "CONTENT";

Todo todo = new Todo();
todo.setTitle(TITLE);
todo.setContent(CONTENT);

MockNotiManager.exception = new RuntimeException();

todoManager.create(todo);
}

private static class MockTodoRepository extends TodoRepository {

private static Todo passedTodo;
Expand All @@ -246,5 +308,17 @@ public Todo store(Todo todo) throws IllegalArgumentException, RepositoryFailedEx

}

private static class MockNotiManager extends NotiManager {

private static String passedTitle;
private static Exception exception;

@Override
public void notify(String title) {
passedTitle = title;
if(exception!=null && exception.getClass()==RuntimeException.class) { throw (RuntimeException)exception; }
}

}

}