๊ธฐ์กด QuestionService๋ ๋ค์๊ณผ ๊ฐ์ด dao ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค.
์ ํํ๋ dao๊ฐ์ฒด๊ฐ Service์ ๊ฐํ๊ฒ ๊ฒฐํฉํ ํํ์ด๋ฉฐ ๊ฐํ ๊ฒฐํฉ์ผ๋ก ์ธํด DB๋ฅผ ์ ์ธํ ์์ ์๋น์ค ์ฝ๋๋ฅผ ํ ์คํธํ๊ธฐ ์ฝ์ง ์๋ค.
QuestionService ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํ์ํ dao์ ๋ํ ์์กด์ฑ์ ์ฃผ์ ํ๋๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
@ExtendWith(MockitoExtension.class)
class QuestionServiceTest {
@InjectMocks
QuestionService questionService;
@Mock
QuestionDao questionDao;
@Mock
AnswerDao answerDao;
@Test
@DisplayName("์กด์ฌํ์ง ์๋ ์ฌ์ฉ์ ์ง๋ฌธ ์ญ์ ์ ์์ธ ๋ฐ์ ํ
์คํธ")
void deleteNotExistQuestion_fail_withException() {
// given
given(answerDao.findAllByQuestionId(1L)).willReturn(Collections.emptyList());
given(questionDao.findById(1L)).willReturn(null);
// then
Assertions.assertThatThrownBy(
() -> questionService.deleteQuestion(1L, new User("test", "1234", "name", "[email protected]")))
.isInstanceOf(CannotDeleteQuestionException.class)
.hasMessageContaining("์กด์ฌํ์ง ์๋ ์ง๋ฌธ๊ธ์ ์ญ์ ํ ์ ์์ต๋๋ค.");
}
@Test
@DisplayName("ํ์ธ์ด ์์ฑํ ์ง๋ฌธ ์ญ์ ์ ์์ธ ๋ฐ์ ํ
์คํธ")
void deleteNotSameUserQuestion_fail_withException() {
// given
given(answerDao.findAllByQuestionId(1L)).willReturn(Collections.emptyList());
given(questionDao.findById(1L)).willReturn(new Question(1L, "differentWriter", "test", "test", new Date(), 0));
// then
Assertions.assertThatThrownBy(
() -> questionService.deleteQuestion(1L, new User("test", "1234", "name", "[email protected]")))
.isInstanceOf(CannotDeleteQuestionException.class)
.hasMessageContaining("๋ค๋ฅธ ์ฌ์ฉ์์ ๊ธ์ ์ญ์ ํ ์ ์์ต๋๋ค.");
}
@Test
@DisplayName("๋ค๋ฅธ ์ฌ์ฉ์์ ์ง๋ฌธ์ด ๋ฌ๋ ค์๋ ์ง๋ฌธ ์ญ์ ์ ์์ธ ๋ฐ์ ํ
์คํธ")
void deleteQuestionWithOtherUserAnswer_fail_withException() {
// given
given(answerDao.findAllByQuestionId(1L)).willReturn(List.of(new Answer(1L, "differentWriter", "test", new Date(), 1L)));
given(questionDao.findById(1L)).willReturn(new Question(1L, "test", "test", "test", new Date(), 0));
// then
Assertions.assertThatThrownBy(
() -> questionService.deleteQuestion(1L, new User("test", "1234", "name", "[email protected]")))
.isInstanceOf(CannotDeleteQuestionException.class)
.hasMessageContaining("์ง๋ฌธ์ ๋ค๋ฅธ ์ฌ์ฉ์์ ๋ต๋ณ์ด ๋ฌ๋ ค์์ผ๋ฏ๋ก ์ญ์ ํ ์ ์์ต๋๋ค.");
}
@Test
@DisplayName("๋ต๋ณ์ด ์๋ ์ฌ์ฉ์์ ์ง๋ฌธ ์ญ์ ์ ์ ์ ๋์ ํ
์คํธ")
void deleteQuestionWithNoAnswer_success() {
// given
given(answerDao.findAllByQuestionId(1L)).willReturn(Collections.emptyList());
given(questionDao.findById(1L)).willReturn(new Question(1L, "test", "test", "test", new Date(), 0));
// then
Assertions.assertThatNoException().isThrownBy(
() -> questionService.deleteQuestion(1L, new User("test", "1234", "name", "[email protected]")));
}
@Test
@DisplayName("์์ ์ด ๋ต๋ณ์ ๋จ ์ง๋ฌธ ์ญ์ ์ ์ ์ ๋์ ํ
์คํธ")
void deleteQuestionWithAnswers_success() {
// given
given(answerDao.findAllByQuestionId(1L)).willReturn(List.of(new Answer(1L, "test", "test", new Date(), 1L)));
given(questionDao.findById(1L)).willReturn(new Question(1L, "test", "test", "test", new Date(), 0));
// then
Assertions.assertThatNoException().isThrownBy(
() -> questionService.deleteQuestion(1L, new User("test", "1234", "name", "[email protected]")));
}
}
DI๋ฅผ ์ ์ฉํ QuestionService์์ Mockito๋ฅผ ์ด์ฉํ์ฌ ์ค์ DB์ ์ฐ๊ฒฐํ์ง ์๊ณ , ์์ ์๋น์ค ์ฝ๋๋ง ๊ฒฉ๋ฆฌํด ํ ์คํธํ ์ ์๋ค.
๊ธฐ์กด ๋ ๊ฑฐ์ ์ปจํธ๋กค๋ฌ, New MVC ์ปจํธ๋กค๋ฌ ๋ฐ Legacy Handler Mapping ์ฝ๋๋ค์ ์์ฑ์ ์์กด์ฑ ์ฃผ์ ์ด ์ ์ฉ๋์ง ์์ ์ํ์ด๋ฏ๋ก ์ด ๋ํ ์์ฑ์๋ก ์ฃผ์ ๋ฐ์ ์ ์๊ฒ ์์ ํ๋ค.
LegacyHandlerMapping, LegacyController