- ์ด์ ๊ธ ํ์ธ -
1. ArugmentCaptor
- ๋ฉ์๋์ ๋ค์ด๊ฐ๋ ์ธ์๊ฐ ๊ฒ์ฆํ ๋ ์ฌ์ฉ์ ํฉ๋๋ค.
- EX)ArgumentCaptor<ํด๋์ค> studentScoreArgumentCaptor = ArgumentCaptor.forClass(ํด๋์ค.class);
1) saveScoreMockTest
- studentScoreService.saveScore.. 3-2 ํธ์์ ๋ง๋ ๋ฉ์๋๋ฅผ ๊ฒ์ฆํ๋ ์์ ์ ์ํํ๊ฒ ์ต๋๋ค.
- ์ฐธ์กฐ -
- DATA ์ ์ฅํ๊ธฐ์ ์ ์ธ์๊ฐ์ ํ์ธํ๊ธฐ ์ํด์ ์ฌ์ฉํฉ๋๋ค.
@Test
@DisplayName("์ฑ์ ์ ์ฅ ๋ก์ง ๊ฒ์ฆ / 60์ ์ด์์ธ ๊ฒฝ์ฐ")
public void saveScoreMockTest() {
// given : ํ๊ท ์ ์๊ฐ 60์ ์ด์์ธ ๊ฒฝ์ฐ
StudentFailRepository studentFailRepository = Mockito.mock(StudentFailRepository.class);
StudentScoreRepository studentScoreRepository = Mockito.mock(StudentScoreRepository.class);
StudentPassRepository studentPassRepository = Mockito.mock(StudentPassRepository.class);
StudentScoreService studentScoreService = new StudentScoreService(
studentFailRepository,
studentScoreRepository,
studentPassRepository
);
String givenStudentName = "pcy";
String givenExam = "testexam";
Integer givenKorScore = 80;
Integer givenEnglishScore = 100;
Integer givenMathScore = 60;
ArgumentCaptor<StudentScore> studentScoreArgumentCaptor = ArgumentCaptor.forClass(StudentScore.class);
ArgumentCaptor<StudentPass> studentPassArgumentCaptor = ArgumentCaptor.forClass(StudentPass.class);
// studentScore ์์ ์ธ์๊ฐ
StudentScore expectStudentScore = StudentScore
.builder()
.studentName(givenStudentName)
.exam(givenExam)
.korScore(givenKorScore)
.englishScore(givenEnglishScore)
.mathScore(givenMathScore)
.build();
// studentPass ์์ ์ธ์๊ฐ
StudentPass expectStudentPass = StudentPass
.builder()
.studentName(givenStudentName)
.exam(givenExam)
.avgScore(
new MyCalculator(0.0)
.add(givenKorScore.doubleValue())
.add(givenEnglishScore.doubleValue())
.add(givenMathScore.doubleValue())
.divide(3.0)
.getResult()
)
.build();
// when
studentScoreService.saveScore(
givenStudentName,
givenExam,
givenKorScore,
givenEnglishScore,
givenMathScore
);
// then
Mockito.verify(studentScoreRepository, Mockito.times(1)).save(studentScoreArgumentCaptor.capture());
Assertions.assertEquals(studentScoreArgumentCaptor.getValue(), expectStudentScore);
Mockito.verify(studentPassRepository, Mockito.times(1)).save(studentPassArgumentCaptor.capture());
Assertions.assertEquals(studentPassArgumentCaptor.getValue(), expectStudentPass);
Mockito.verify(studentFailRepository, Mockito.times(0)).save(Mockito.any());
}
- ArgumentCaptor<StudentScore> studentScoreArgumentCaptor = ArgumentCaptor.forClass(StudentScore.class)
-> ์ธ์ํ์ธํ DTO๋ฅผ ์์ ๊ฐ์ด ArgumentCaptor ์ ์ธ์ ํฉ๋๋ค.
- ~.save(studentScoreArgumentCaptor.capture())
-> Mockito ๋ฌธ๊ตฌ ๋ง์ง๋ง์ ์์ ๊ฐ์ด ์์ฑํ์ฌ ์ธ์๋ค์ ์บก์ณ ํฉ๋๋ค.
- Assertions.assertEquals(studentScoreArgumentCaptor.getValue(), expectStudentScore);
-> ์์ํ ๊ฐ๊ณผ ๋ง๋์ง ๋น๊ตํ์ฌ ํ ์คํธ ํฉ๋๋ค.
* ๊ฐ์ด ์๋ ํด๋์ค๋ณ๋ก ๋น๊ตํ ์ ์๋ ๊ฐ DTO์ @DATA ์ด๋ ธํ ์ด์ ํน์ @EqualsAndHashCode, @Getter๋ฅผ ์ ์ธํด์ฃผ์ด์ผ ํฉ๋๋ค.
2. ์ฐธ๊ณ
- SpringBoot์ DTO ์์ ๋ง์ด ์ฌ์ฉ๋๋ ์ด๋ ธํ ์ด์ ์ ์ค๋ช ํ URL์ ์๋ ค๋๋ฆฝ๋๋ค.
- ์ฐธ์กฐ : ํฐ์คํ ๋ฆฌ ๊ฐ์ธ ๋ธ๋ก๊ทธ
3. Github์ผ๋ก ํ์ธ
์ ์ฒด ํ์ผ ํ์ธ(ํ์ฌ ๋ณ๊ฒฝ๋ด์ญ)
[Github]
์ฐธ์กฐ : ์ธํ๋ฐ ๊ฐ์ [์ฅฌ์ฅฌ์ ํจ๊ป ํ๋ฃจ๋ง์ ๋๋ด๋ ์คํ๋ง ํ ์คํธ]
'๐ป FrameWork(ํ๋ ์์ํฌ) > SpringTEST(์คํ๋งํ ์คํธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
4-1 [ํตํฉTEST] testcontainers ์ฌ์ฉํ๊ธฐ ์ํ ์ค์ ๋ฐ ๊ฐ๋จํ DB TEST (0) | 2024.06.15 |
---|---|
3-6 [Mocktio] ๋ฆฌํฉํ ๋ง ์์ (0) | 2024.06.07 |
3-4 [Mocktio] Stubbing ํ ์คํธ(๊ฐ์ง ๋ฐ์ดํฐ ์์ฑ) (0) | 2024.06.06 |
3-3 [Mocktio] ํ์ ๊ฒ์ฆ TEST(๋ฉ์๋ ํธ์ถ ์ฌ๋ถ ํ์ธ) (0) | 2024.06.06 |
3-2 [Mocktio] ๊ฐ๋จํ ์ฑ์ ์ ์ฅ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ (0) | 2024.06.04 |