- μ΄μ κΈ νμΈ -
1. λ°λ³΅λμ΄μ μμ±λλ Mock κ°μ²΄ μ²λ¦¬
- μ΄μ μκ°μ μμ νλ StudentScoreServiceMockTest κ°μ§κ³ μμ νλλ‘ νκ² μ΅λλ€.
- StudentScoreServiceMockTest μ½λ URLμ μλμ μμ΅λλ€.
- @BeforeEach μ΄λ Έν μ΄μ μ μ¬μ©νμ¬ μμ±ν©λλ€.
public class StudentScoreServiceMockTest {
private StudentScoreService studentScoreService;
private StudentScoreRepository studentScoreRepository;
private StudentPassRepository studentPassRepository;
private StudentFailRepository studentFailRepository;
@BeforeEach
public void beforeEach() {
studentFailRepository = Mockito.mock(StudentFailRepository.class);
studentScoreRepository = Mockito.mock(StudentScoreRepository.class);
studentPassRepository = Mockito.mock(StudentPassRepository.class);
studentScoreService = new StudentScoreService(
studentFailRepository,
studentScoreRepository,
studentPassRepository
);
}
.....
- κ° ν μ€νΈκ° μνλκΈ° μ μ beforeEach λ¨Όμ μνμ΄ λ©λλ€.
2. Test Data Builder ν¨ν΄
- Test Data Builder ν¨ν΄μ ν μ€νΈ μ½λμμ μ¬μ©λλ λμμΈ ν¨ν΄ μ€ νλμ λλ€.
- 볡μ‘ν ν μ€νΈ λ°μ΄ν°λ₯Ό μ½κ³ κ°κ²°νκ² λ§λ€μ΄μ£Όλ ν¨ν΄μ λλ€.
- κ°μ²΄λ₯Ό μμ±νκ³ μ΄κΈ°ννλ κ³Όμ μ λ¨μννκ³ κ°λ μ±μ λμ΄λ λ° λμμ΄ λ©λλ€.
1) StudentScoreTestDataBuilder
- μμΉ : com.pcy.dayonetest.model
public class StudentScoreTestDataBuilder {
public static StudentScore.StudentScoreBuilder passed() {
return StudentScore
.builder()
.korScore(80)
.englishScore(100)
.mathScore(90)
.studentName("defaultName")
.exam("defaultExam");
}
public static StudentScore.StudentScoreBuilder failed() {
return StudentScore
.builder()
.korScore(50)
.englishScore(40)
.mathScore(30)
.studentName("defaultName")
.exam("defaultExam");
}
}
2) saveScoreMockTest() μμ
- μμΉ : com.pcy.dayonetest.service.StudentScoreServiceMockTest
@Test
@DisplayName("μ±μ μ μ₯ λ‘μ§ κ²μ¦ / 60μ μ΄μμΈ κ²½μ°")
public void saveScoreMockTest() {
// given : νκ· μ μκ° 60μ μ΄μμΈ κ²½μ°
ArgumentCaptor<StudentScore> studentScoreArgumentCaptor = ArgumentCaptor.forClass(StudentScore.class);
ArgumentCaptor<StudentPass> studentPassArgumentCaptor = ArgumentCaptor.forClass(StudentPass.class);
// studentScore μμ μΈμκ°
StudentScore expectStudentScore = StudentScoreTestDataBuilder.passed()
.studentName("pcy") // μ€λ²λΌμ΄λ© μ¬μ© κ°λ₯
.build();
// studentPass μμ μΈμκ°
StudentPass expectStudentPass = StudentPass
.builder()
.studentName(expectStudentScore.getStudentName())
.exam(expectStudentScore.getExam())
.avgScore(
new MyCalculator(0.0)
.add(expectStudentScore.getEnglishScore().doubleValue())
.add(expectStudentScore.getKorScore().doubleValue())
.add(expectStudentScore.getMathScore().doubleValue())
.divide(3.0)
.getResult()
)
.build();
// when
studentScoreService.saveScore(
expectStudentScore.getStudentName(),
expectStudentScore.getExam(),
expectStudentScore.getKorScore(),
expectStudentScore.getEnglishScore(),
expectStudentScore.getMathScore()
);
// 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());
}
- expectStudentScore λ³μμ ꡬνλλ λ°©λ²μ΄ λ³κ²½μ΄ λμμ΅λλ€.
- Test Data Builder μ€λ²λΌμ΄λ©μ΄ κ°λ₯νμ¬ κ°μμ κ°μ λ§κ² λ³ννμ¬ μ¬μ©μ΄ κ°λ₯ν©λλ€.
3. Fixture Object ν¨ν΄ (feat. TestFixture)
- Fixture Object ν¨ν΄μ ν μ€νΈμμ μ¬μ©λλ κ³΅ν΅ λ°μ΄ν°μ κ°μ²΄λ€μ λ³λμ Fixture ν΄λμ€λ‘ λΆλ¦¬νμ¬ κ΄λ¦¬νλ ν¨ν΄μ λλ€.
- ν μ€νΈ λ°μ΄ν°λ₯Ό μμ±νκ³ μ΄κΈ°ννλ μν μ μνν©λλ€.
- ν μ€νΈ λ°μ΄ν°μ κ΄λ¦¬κ° νΈλ¦¬ν΄μ§λ©°, ν μ€νΈ νκ²½ μ€μ κ³Ό μ 리 μμ μ ν¨κ³Όμ μΌλ‘ μ²λ¦¬ν μ μμ΅λλ€.
1) StudentFailFixture
- μμΉ : com.pcy.dayonetest.model
public class StudentFailFixture {
public static StudentFail create(StudentScore studentScore) {
MyCalculator myCalculator = new MyCalculator(0.0);
return StudentFail
.builder()
.studentName(studentScore.getStudentName())
.exam(studentScore.getExam())
.avgScore(
myCalculator
.add(studentScore.getKorScore().doubleValue())
.add(studentScore.getEnglishScore().doubleValue())
.add(studentScore.getMathScore().doubleValue())
.divide(3.0)
.getResult()
).build();
}
public static StudentFail create(String studentName, String exam) {
return StudentFail
.builder()
.studentName(studentName)
.exam(exam)
.avgScore(40.0)
.build();
}
}
2) saveScoreMockTest2() μμ
- μμΉ : com.pcy.dayonetest.service.StudentScoreServiceMockTest
@Test
@DisplayName("μ±μ μ μ₯ λ‘μ§ κ²μ¦ / 60μ λ―Έλ§μΈ κ²½μ°")
public void saveScoreMockTest2() {
// given : νκ· μ μκ° 60μ λ―Έλ§μΈ κ²½μ°
// studentScore
StudentScore expectStudentScore = StudentScoreTestDataBuilder.failed()
.studentName("pcy") // μ€λ²λΌμ΄λ© μ¬μ© κ°λ₯
.build();
// studentFail
StudentFail expectStudentFail = StudentFailFixture.create(expectStudentScore);
ArgumentCaptor<StudentScore> studentScoreArgumentCaptor = ArgumentCaptor.forClass(StudentScore.class);
ArgumentCaptor<StudentFail> studentFailArgumentCaptor = ArgumentCaptor.forClass(StudentFail.class);
// when
studentScoreService.saveScore(
expectStudentScore.getStudentName(),
expectStudentScore.getExam(),
expectStudentScore.getKorScore(),
expectStudentScore.getEnglishScore(),
expectStudentScore.getMathScore()
);
// then
Mockito.verify(studentScoreRepository, Mockito.times(1)).save(studentScoreArgumentCaptor.capture());
Assertions.assertEquals(studentScoreArgumentCaptor.getValue(), expectStudentScore);
Mockito.verify(studentPassRepository, Mockito.times(0)).save(Mockito.any());
Mockito.verify(studentFailRepository, Mockito.times(1)).save(studentFailArgumentCaptor.capture());
Assertions.assertEquals(studentFailArgumentCaptor.getValue(), expectStudentFail);
}
- expectStudentFailλ³μμ ꡬνλλ λ°©λ²μ΄ λ³κ²½μ΄ λμμ΅λλ€.
- μ΄μ μ½λμλ λ€λ₯΄κ² κ°κ²°νκ² κ΅¬νμ΄ κ°λ₯νκ³ κ΄λ¦¬κ° μ½μ΅λλ€.
4. GithubμΌλ‘ νμΈ
μ 체 νμΌ νμΈ(νμ¬ λ³κ²½λ΄μ)
[Github]
μ°Έμ‘° : μΈνλ° κ°μ [μ₯¬μ₯¬μ ν¨κ» ν루λ§μ λλ΄λ μ€νλ§ ν μ€νΈ]
'π» FrameWork(νλ μμν¬) > SpringTEST(μ€νλ§ν μ€νΈ)' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
4-2 [ν΅ν©TEST] μ€μ DATAλ‘ μ§ννλ TEST (0) | 2024.06.16 |
---|---|
4-1 [ν΅ν©TEST] testcontainers μ¬μ©νκΈ° μν μ€μ λ° κ°λ¨ν DB TEST (0) | 2024.06.15 |
3-5 [Mocktio] ArugmentCaptor (λ°μ΄ν° μ λ ₯ μΈμ νμΈ) (0) | 2024.06.06 |
3-4 [Mocktio] Stubbing ν μ€νΈ(κ°μ§ λ°μ΄ν° μμ±) (0) | 2024.06.06 |
3-3 [Mocktio] νμ κ²μ¦ TEST(λ©μλ νΈμΆ μ¬λΆ νμΈ) (0) | 2024.06.06 |