- ์ด์ ๊ธ ํ์ธ -
1. Mockito.when().thenReturn()
- ๋ฉ์๋๋ฅผ ์ค์ ํธ์ถํ์ง๋ง ๋ฆฌํด ๊ฐ์ ์์๋ก ์ ์ ํ ์ ์์ต๋๋ค.
- ๋ฉ์๋ ์์ ์ด ์ค๋ ๊ฑธ๋ฆด ๊ฒฝ์ฐ ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ ค์ผ ํฉ๋๋ค.
- ์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ธฐ ๋๋ฌธ์ ๋์ ๋ฉ์๋์ ๋ฌธ์ ์ ์ด ์์ ๊ฒฝ์ฐ ๋ฐ๊ฒฌ ํ ์ ์์ต๋๋ค.
2. Stubbing ํ ์คํธ(๊ฐ์ง ๋ฐ์ดํฐ ์์ฑ)
- studentScoreService.getPassStudentsList.. 3-2 ํธ์์ ๋ง๋ ๋ฉ์๋๋ฅผ ๊ฒ์ฆํ๋ ์์ ์ ์ํํ๊ฒ ์ต๋๋ค.
- ์ฐธ์กฐ -
1) getPassStudentsListTest
- ํฉ๊ฒฉ์ ๋ช ๋จ ์กฐํ ๊ฒ์ฆ
@Test
@DisplayName("ํฉ๊ฒฉ์ ๋ช
๋จ ์กฐํ ๊ฒ์ฆ")
public void getPassStudentsListTest() {
// given
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 givenTestExam = "testexam";
StudentPass es1 = StudentPass.builder().id(1L).studentName("pcy").exam(givenTestExam).avgScore(70.0).build();
StudentPass es2 = StudentPass.builder().id(2L).studentName("test").exam(givenTestExam).avgScore(80.0).build();
StudentPass nes1 = StudentPass.builder().id(3L).studentName("test").exam("mathexam").avgScore(90.0).build();
// ๊ฐ์ง ๋ฐ์ดํฐ ์์ฑ
Mockito.when(studentPassRepository.findAll()).thenReturn(List.of(
es1,
es2,
nes1
));
// when
List<ExamPassStudentResponse> expectResponses = List.of(es1, es2)
.stream()
.map((pass) -> new ExamPassStudentResponse(pass.getStudentName(), pass.getAvgScore()))
.toList();
List<ExamPassStudentResponse> responses = studentScoreService.getPassStudentsList(givenTestExam);
// then
Assertions.assertIterableEquals(expectResponses, responses);
}
- Mockito.when().thenReturn() ๋ฅผ ์ฌ์ฉํ์ฌ ํฉ๊ฒฉ์ ๋ช ๋จ ์กฐํํ๋ ๋ฉ์๋ ํธ์ถ ์ ์ฌ์ฉ๋๋ ๊ฐ์ง ๋ฐ์ดํฐ๋ฅผ ์์ฑํฉ๋๋ค.
- expectResponses ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ TEST๋ก ํธ์ถ๋๋ ๋ฐ์ดํฐ์ ๋น๊ต๋ฅผ ํฉ๋๋ค.
- Assertions.assertIterableEquals(expectResponses, responses) ๋ฉ์๋ ์ฌ์ฉ ์ ์ response DTO ์์@EqualsAndHashCode, @Getter ํน์ @Data ์ด๋ ธํ ์ด์ ์ ์ ์ ํด์ฃผ์ด์ผํฉ๋๋ค.
@EqualsAndHashCode
@Getter
@AllArgsConstructor
public class ExamFailStudentResponse {
private final String studentName;
private final Double avgScore;
}
- ์ ์์ผ ๊ฒฝ์ฐ ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ ํ ์ ์์ต๋๋ค.
- ๋ง์ฝ ๋ก์ง์ด ์๋ชป๋๊ฑฐ๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ชป ์์ฑํ ๋ฐ์ดํฐ๋ก ํ ์คํธํ ๊ฒฝ์ฐ ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ ํ ์ ์์ต๋๋ค.
3. Github์ผ๋ก ํ์ธ
์ ์ฒด ํ์ผ ํ์ธ(ํ์ฌ ๋ณ๊ฒฝ๋ด์ญ)
[Github]
์ฐธ์กฐ : ์ธํ๋ฐ ๊ฐ์ [์ฅฌ์ฅฌ์ ํจ๊ป ํ๋ฃจ๋ง์ ๋๋ด๋ ์คํ๋ง ํ ์คํธ]
'๐ป FrameWork(ํ๋ ์์ํฌ) > SpringTEST(์คํ๋งํ ์คํธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
3-6 [Mocktio] ๋ฆฌํฉํ ๋ง ์์ (0) | 2024.06.07 |
---|---|
3-5 [Mocktio] ArugmentCaptor (๋ฐ์ดํฐ ์ ๋ ฅ ์ธ์ ํ์ธ) (0) | 2024.06.06 |
3-3 [Mocktio] ํ์ ๊ฒ์ฆ TEST(๋ฉ์๋ ํธ์ถ ์ฌ๋ถ ํ์ธ) (0) | 2024.06.06 |
3-2 [Mocktio] ๊ฐ๋จํ ์ฑ์ ์ ์ฅ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ (0) | 2024.06.04 |
3-1 [Mocktio] ๊ฐ๋จํ ์ฑ์ ์ ์ฅ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ(๋ก์ปฌ ํ๊ฒฝ ๊ตฌ์ฑ) (0) | 2024.06.03 |