@Mock
์ ์
โข
๋จ์ ํ
์คํธ๋ฅผ ์ํ ๊ฐ์ง(Mock) ๊ฐ์ฒด๋ฅผ ์์ฑ
โข
์ค์ ๊ฐ์ฒด ์์ด๋ ํ
์คํธ ๊ฐ๋ฅ
ํน์ง
โข
๋ชจ๋ ๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ฌด ๋์๋ ํ์ง ์์
โข
when().thenReturn() ๋ฑ์ผ๋ก ์ํ๋ ๋์ ์ ์ ํ์
โข
๋จ์ ํ
์คํธ์์ ์ฃผ๋ก ์ฌ์ฉ
@ExtendWith(MockitoExtension.class)
class MailServiceTest {
@Mock
private MailSendClient mailSendClient;
@Mock
private MailSendHistoryRepository mailSendHistoryRepository;
@InjectMocks
private MailService mailService;
@DisplayName("๋ฉ์ผ ์ ์ก ํ
์คํธ - Mock ์ฌ์ฉ")
@Test
void sendMailTest() {
// given
String to = "test@test.com";
String subject = "ํ
์คํธ";
String content = "ํ
์คํธ ๋ด์ฉ";
String fromEmail = "from@test.com";
//๋์ ์ ์
when(mailSendClient.sendEmail(anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
// when
boolean result = mailService.sendEmail(to, subject, content, fromEmail);
// then
assertThat(result).isTrue();
verify(mailSendClient, times(1))
.sendEmail(to, subject, content, fromEmail);
}
}
Java
๋ณต์ฌ
@MockBean
์ ์
โข
์คํ๋ง ์ปจํ
์คํธ์์ ํน์ ๋น์ ๊ฐ์ง ๊ฐ์ฒด๋ก ๋์ฒด
โข
ํตํฉ ํ
์คํธ์์ ์ฌ์ฉ
ํน์ง
โข
์คํ๋ง ๋ถํธ ํ
์คํธ์์ ์ฌ์ฉ
โข
ApplicationContext์ Mock ๊ฐ์ฒด๋ฅผ ๋ฑ๋ก
โข
๊ธฐ์กด์ ๋ฑ๋ก๋ ๋น์ Mock์ผ๋ก ๊ต์ฒด
@SpringBootTest
class MailServiceIntegrationTest {
@MockBean
private MailSendClient mailSendClient;
@Autowired
private MailService mailService;
@DisplayName("๋ฉ์ผ ์ ์ก ํ
์คํธ - MockBean ์ฌ์ฉ")
@Test
void sendMailTest() {
// given
String to = "test@test.com";
String subject = "ํ
์คํธ";
String content = "ํ
์คํธ ๋ด์ฉ";
String fromEmail = "from@test.com";
//๋์ ์ ์
when(mailSendClient.sendEmail(anyString(), anyString(), anyString(), anyString()))
.thenReturn(true);
// when
boolean result = mailService.sendEmail(to, subject, content, fromEmail);
// then
assertThat(result).isTrue();
}
}
Java
๋ณต์ฌ
@InjectMocks
์ ์
โข
@Mock์ผ๋ก ์์ฑ๋ ๊ฐ์ฒด๋ค์ ์๋์ผ๋ก ์ฃผ์
โข
์์กด์ฑ ์ฃผ์
(DI)์ ํ
์คํธ์์ ๊ตฌํ
ํน์ง
โข
@Mock ๊ฐ์ฒด๋ค์ ์๋์ผ๋ก ์ฃผ์
๋ฐ์
โข
์์ฑ์, ์ธํฐ, ํ๋ ์ฃผ์
์ง์
โข
์ฌ๋ฌ Mock ๊ฐ์ฒด๋ค์ ์์กด ๊ด๊ณ ์ค์ ์ ์ ์ฉ
@Spy
์ ์
โข
์ค์ ๊ฐ์ฒด์ ์ผ๋ถ ๋ฉ์๋๋ง Mock์ผ๋ก ์ง์
โข
๋๋จธ์ง๋ ์ค์ ๊ฐ์ฒด์ ๋ฉ์๋ ์ฌ์ฉ
ํน์ง
โข
์ค์ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ ํ์ ์ผ๋ก ์ค๋ฒ๋ผ์ด๋
โข
ํน์ ๋ฉ์๋๋ง ์คํฐ๋นํ๊ณ ์ถ์ ๋ ์ ์ฉ
โข
๋ถ๋ถ์ ์ธ Mock์ด ํ์ํ ๋ ์ฌ์ฉ
@ExtendWith(MockitoExtension.class)
class MailServiceTest {
@Spy
private MailSendClient mailSendClient;
@Mock
private MailSendHistoryRepository mailSendHistoryRepository;
@InjectMocks
private MailService mailService;
@DisplayName("๋ฉ์ผ ์ ์ก ํ
์คํธ - Spy ์ฌ์ฉ")
@Test
void sendMailTest() {
// given
String to = "test@test.com";
String subject = "ํ
์คํธ";
String content = "ํ
์คํธ ๋ด์ฉ";
String fromEmail = "from@test.com";
// ํน์ ๋ฉ์๋๋ง ์คํฐ๋น
doReturn(true)
.when(mailSendClient)
.sendEmail(anyString(), anyString(), anyString(), anyString());
// when
boolean result = mailService.sendEmail(to, subject, content, fromEmail);
// then
assertThat(result).isTrue();
// ์ค์ ํธ์ถ ์ฌ๋ถ ํ์ธ
verify(mailSendClient, times(1))
.sendEmail(to, subject, content, fromEmail);
}
}
Java
๋ณต์ฌ
@SpyBean
์ ์
โข
์คํ๋ง ๋น์ ์ผ๋ถ ๋ฉ์๋๋ง Mock์ผ๋ก ๊ต์ฒด
โข
๋๋จธ์ง๋ ์ค์ ๋น์ ๋์ ์ ์ง
ํน์ง
โข
์คํ๋ง ์ปจํ
์คํธ์์ ๋์
โข
์ค์ ๋น์ ์ผ๋ถ ๋์๋ง ์์ ๊ฐ๋ฅ
โข
ํตํฉ ํ
์คํธ์์ ๋ถ๋ถ์ ์ธ Mock์ด ํ์ํ ๋ ์ฌ์ฉ
@SpringBootTest
class MailServiceIntegrationTest {
@SpyBean
private MailSendClient mailSendClient;
@Autowired
private MailService mailService;
@DisplayName("๋ฉ์ผ ์ ์ก ํ
์คํธ - SpyBean ์ฌ์ฉ")
@Test
void sendMailTest() {
// given
String to = "test@test.com";
String subject = "ํ
์คํธ";
String content = "ํ
์คํธ ๋ด์ฉ";
String fromEmail = "from@test.com";
// ํน์ ๋ฉ์๋๋ง ์คํฐ๋นํ๊ณ ๋๋จธ์ง๋ ์ค์ ๋์ ์ํ
doReturn(true)
.when(mailSendClient)
.sendEmail(anyString(), anyString(), anyString(), anyString());
// when
boolean result = mailService.sendEmail(to, subject, content, fromEmail);
// then
assertThat(result).isTrue();
}
}
Java
๋ณต์ฌ
๋ฌธ์
์๋ 3๊ฐ์ ํ
์คํธ๊ฐ ์์ต๋๋ค.
๋ด์ฉ์ ์ดํด๋ณด๊ณ , ๊ฐ ํญ๋ชฉ์ @BeforeEach, given์ , when์ ์ ๋ฐฐ์นํ๋ค๋ฉด ์ด๋ป๊ฒ ๋ฐฐ์นํ๊ณ ์ถ์ผ์ ๊ฐ์?
(@BeforeEach์ ์ฌ๋ผ๊ฐ ๋ด์ฉ์ ๊ณตํต ํญ๋ชฉ์ผ๋ก ํฉ์น ์ ์์ต๋๋ค. ex. 1-1๊ณผ 2-1์ ํ๋๋ก ํฉ์ณ์ @BeforeEach์ ๋ฐฐ์น)
@BeforeEach
void setUp() {
โ
}
@DisplayName("์ฌ์ฉ์๊ฐ ๋๊ธ์ ์์ฑํ ์ ์๋ค.")
@Test
void writeComment() {
1-1. ์ฌ์ฉ์ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
1-2. ์ฌ์ฉ์ ์์ฑ
1-3. ๊ฒ์๋ฌผ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
1-4. ๊ฒ์๋ฌผ ์์ฑ
1-5. ๋๊ธ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
1-6. ๋๊ธ ์์ฑ
// given
โ
// when
โ
// then
๊ฒ์ฆ
}
@DisplayName("์ฌ์ฉ์๊ฐ ๋๊ธ์ ์์ ํ ์ ์๋ค.")
@Test
void updateComment() {
2-1. ์ฌ์ฉ์ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
2-2. ์ฌ์ฉ์ ์์ฑ
2-3. ๊ฒ์๋ฌผ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
2-4. ๊ฒ์๋ฌผ ์์ฑ
2-5. ๋๊ธ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
2-6. ๋๊ธ ์์ฑ
2-7. ๋๊ธ ์์
// given
โ
// when
โ
// then
๊ฒ์ฆ
}
@DisplayName("์์ ์ด ์์ฑํ ๋๊ธ์ด ์๋๋ฉด ์์ ํ ์ ์๋ค.")
@Test
void cannotUpdateCommentWhenUserIsNotWriter() {
3-1. ์ฌ์ฉ์1 ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
3-2. ์ฌ์ฉ์1 ์์ฑ
3-3. ์ฌ์ฉ์2 ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
3-4. ์ฌ์ฉ์2 ์์ฑ
3-5. ์ฌ์ฉ์1์ ๊ฒ์๋ฌผ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
3-6. ์ฌ์ฉ์1์ ๊ฒ์๋ฌผ ์์ฑ
3-7. ์ฌ์ฉ์1์ ๋๊ธ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น
3-8. ์ฌ์ฉ์1์ ๋๊ธ ์์ฑ
3-9. ์ฌ์ฉ์2๊ฐ ์ฌ์ฉ์1์ ๋๊ธ ์์ ์๋
// given
โ
// when
โ
// then
๊ฒ์ฆ
}
Java
๋ณต์ฌ
@BeforeEach
void setUp() {
(1-1. ์ฌ์ฉ์ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น ,3-1. ์ฌ์ฉ์1 ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น, 3-3. ์ฌ์ฉ์2 ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น)
(1-3. ๊ฒ์๋ฌผ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น , 3-5. ์ฌ์ฉ์1์ ๊ฒ์๋ฌผ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น)
(1-5. ๋๊ธ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น, 3-7. ์ฌ์ฉ์1์ ๋๊ธ ์์ฑ์ ํ์ํ ๋ด์ฉ ์ค๋น)
}
@DisplayName(""์ฌ์ฉ์๊ฐ ๋๊ธ์ ์์ฑํ ์ ์๋ค."")
@Test
void writeComment() {
// given
1-2. ์ฌ์ฉ์ ์์ฑ
1-4. ๊ฒ์๋ฌผ ์์ฑ
// when
1-6. ๋๊ธ ์์ฑ
// then
๊ฒ์ฆ
}
@DisplayName(""์ฌ์ฉ์๊ฐ ๋๊ธ์ ์์ ํ ์ ์๋ค."")
@Test
void updateComment() {
// given
2-2. ์ฌ์ฉ์ ์์ฑ
2-4. ๊ฒ์๋ฌผ ์์ฑ
2-6. ๋๊ธ ์์ฑ
// when
2-7. ๋๊ธ ์์
// then
๊ฒ์ฆ
}
@DisplayName(""์์ ์ด ์์ฑํ ๋๊ธ์ด ์๋๋ฉด ์์ ํ ์ ์๋ค."")
@Test
void cannotUpdateCommentWhenUserIsNotWriter() {
// given
3-2. ์ฌ์ฉ์1 ์์ฑ
3-4. ์ฌ์ฉ์2 ์์ฑ
3-6. ์ฌ์ฉ์1์ ๊ฒ์๋ฌผ ์์ฑ
3-8. ์ฌ์ฉ์1์ ๋๊ธ ์์ฑ
// when
3-9. ์ฌ์ฉ์2๊ฐ ์ฌ์ฉ์1์ ๋๊ธ ์์ ์๋
// then
๊ฒ์ฆ
}
Java
๋ณต์ฌ


