Skip to content

Commit

Permalink
refactor: when() 메서드를 BDDMockito.given() 메서드로 대체
Browse files Browse the repository at this point in the history
  • Loading branch information
amaran-th committed Sep 8, 2023
1 parent b560495 commit 7a5b91b
Showing 1 changed file with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.BDDMockito;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -49,8 +50,8 @@ class QuizServiceTest {
@Test
void createQuiz_fail_KeywordId() {
//given
when(keywordRepository.findById(anyLong()))
.thenReturn(Optional.empty());
BDDMockito.given(keywordRepository.findById(anyLong()))
.willReturn(Optional.empty());
final QuizRequest question = new QuizRequest("question");

//when,then
Expand All @@ -66,10 +67,10 @@ void createQuiz() {
final Keyword keyword = new Keyword(null, null, null, 2, 0, null, null, null);
final String requestQuestion = "question";

when(keywordRepository.findById(anyLong()))
.thenReturn(Optional.of(keyword));
when(quizRepository.save(any()))
.thenReturn(new Quiz(keyword, null));
BDDMockito.given(keywordRepository.findById(anyLong()))
.willReturn(Optional.of(keyword));
BDDMockito.given(quizRepository.save(any()))
.willReturn(new Quiz(keyword, null));

final ArgumentCaptor<Quiz> quizArgumentCaptor = ArgumentCaptor.forClass(Quiz.class);

Expand All @@ -92,8 +93,8 @@ void findQuizzesByKeywordId() {
//given
final long requestKeywordId = 1L;

when(quizRepository.findFetchQuizByKeywordId(anyLong()))
.thenReturn(Arrays.asList(
BDDMockito.given(quizRepository.findFetchQuizByKeywordId(anyLong()))
.willReturn(Arrays.asList(
new Quiz(null, null),
new Quiz(null, null)
)
Expand All @@ -114,8 +115,8 @@ void findQuizzesByKeywordId() {
@Test
void updateQuiz_fail() {
//given
when(quizRepository.findById(anyLong()))
.thenReturn(Optional.empty());
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.empty());
final QuizRequest quizRequest = new QuizRequest();

//when,then
Expand All @@ -131,8 +132,8 @@ void updateQuiz() {
final String originQuestion = "origin";
final Quiz targetQuiz = new Quiz(null, originQuestion);

when(quizRepository.findById(anyLong()))
.thenReturn(Optional.of(targetQuiz));
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.of(targetQuiz));

//when
final String updatedQuestion = "updated";
Expand All @@ -146,8 +147,8 @@ void updateQuiz() {
@Test
void deleteQuiz_fail() {
//given
when(quizRepository.existsById(anyLong()))
.thenReturn(false);
BDDMockito.given(quizRepository.existsById(anyLong()))
.willReturn(false);

//when,then
assertThatThrownBy(() -> quizService.deleteQuiz(1L))
Expand All @@ -159,8 +160,8 @@ void deleteQuiz_fail() {
@Test
void deleteQuiz() {
//given
when(quizRepository.existsById(anyLong()))
.thenReturn(true);
BDDMockito.given(quizRepository.existsById(anyLong()))
.willReturn(true);

//when
quizService.deleteQuiz(1L);
Expand All @@ -173,8 +174,8 @@ void deleteQuiz() {
@Test
void findById_fail() {
//given
when(quizRepository.findById(anyLong()))
.thenReturn(Optional.empty());
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.empty());

//when,then
assertThatThrownBy(() -> quizService.findById(1L, null))
Expand All @@ -188,8 +189,8 @@ void findById() {
//given
final long findQuizId = 1L;
final String findQuizQuestion = "question";
when(quizRepository.findById(anyLong()))
.thenReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));

//when
final QuizResponse quizResponseById = quizService.findById(1L, null);
Expand All @@ -211,10 +212,10 @@ void findById_isLearning_true() {
//given
final long findQuizId = 1L;
final String findQuizQuestion = "question";
when(essayAnswerRepository.existsByQuizIdAndMemberId(anyLong(), anyLong()))
.thenReturn(true);
when(quizRepository.findById(anyLong()))
.thenReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));
BDDMockito.given(essayAnswerRepository.existsByQuizIdAndMemberId(anyLong(), anyLong()))
.willReturn(true);
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));

//when
final QuizResponse quizResponseById = quizService.findById(1L, 1L);
Expand All @@ -229,10 +230,10 @@ void findById_isLearning_false() {
//given
final long findQuizId = 1L;
final String findQuizQuestion = "question";
when(essayAnswerRepository.existsByQuizIdAndMemberId(anyLong(), anyLong()))
.thenReturn(false);
when(quizRepository.findById(anyLong()))
.thenReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));
BDDMockito.given(essayAnswerRepository.existsByQuizIdAndMemberId(anyLong(), anyLong()))
.willReturn(false);
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));

//when
final QuizResponse quizResponseById = quizService.findById(1L, 1L);
Expand All @@ -247,8 +248,8 @@ void findById_isLearning_false_anonymous() {
//given
final long findQuizId = 1L;
final String findQuizQuestion = "question";
when(quizRepository.findById(anyLong()))
.thenReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));
BDDMockito.given(quizRepository.findById(anyLong()))
.willReturn(Optional.of(new Quiz(findQuizId, null, findQuizQuestion)));

//when
final QuizResponse quizResponseById = quizService.findById(1L, null);
Expand Down

0 comments on commit 7a5b91b

Please sign in to comment.