BDD 구조에 맞게 Mockito 문법을 변화시킨 것
이름만 바꾼것일뿐 동작은 기존 Mockito와 동일하다
기존
@ExtendWith(MockitoExtension.class)
class MailServiceTest {
@Spy
private MailSendClient mailSendClient;
@Mock
private MailSendHistoryRepository mailSendHistoryRepository;
@InjectMocks
private MailService mailService;
@DisplayName("메일 전송 테스트")
@Test
void sendMail() {
// given
when(mailSendClient.sendEmail(anyString(), anyString(), anyString(),
anyString())).thenReturn(true);
// doReturn(true)
// .when(mailSendClient)
// .sendEmail(anyString(), anyString(), anyString(), anyString());
// when
boolean result = mailService.sendMail("", "", "", "");
// then
assertThat(result).isTrue();
Mockito.verify(mailSendHistoryRepository, times(1)).save(any(MailSendHistory.class));
}
}
BDDMocktio 사용
@ExtendWith(MockitoExtension.class)
class MailServiceTest {
@Spy
private MailSendClient mailSendClient;
@Mock
private MailSendHistoryRepository mailSendHistoryRepository;
@InjectMocks
private MailService mailService;
@DisplayName("메일 전송 테스트")
@Test
void sendMail() {
// given
// when(mailSendClient.sendEmail(anyString(), anyString(), anyString(),
// anyString())).thenReturn(true);
BDDMockito.given(mailSendClient.sendEmail(anyString(), anyString(), anyString(),
anyString())).willReturn(true);
// doReturn(true)
// .when(mailSendClient)
// .sendEmail(anyString(), anyString(), anyString(), anyString());
// when
boolean result = mailService.sendMail("", "", "", "");
// then
assertThat(result).isTrue();
Mockito.verify(mailSendHistoryRepository, times(1)).save(any(MailSendHistory.class));
}
}