- ์ด๋ฒ 4-2์์๋ ์ค์ DATA๋ก TEST๋ฅผ ์งํํ๊ฒ ์ต๋๋ค.
- ํ๊ฒฝ์ค์ ๊ด๋ จ ๊ธ์ ์๋์ ์๋ ๊ธ์ ์ฐธ์กฐํ์ฌ ์งํํด์ฃผ์๋ฉด ๋ฉ๋๋ค.
1. StudentScoreServiceIntegrationTest
public class StudentScoreServiceIntegrationTest extends IntegrationTest {
@Autowired
private StudentScoreService studentScoreService;
@Autowired
private EntityManager entityManager;
}
- StudentScoreServiceIntegrationTest ํด๋์ค ์์ฑ ๋ฐ IntegrationTest๋ฅผ ์์๋ฐ์ ์ค๋๋ค.
- @Autowired ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ StudentScoreService์ EntityManager ์ธ์คํด์ค๋ฅผ ์ฃผ์
1) savePassedStudentScoreTest ๋ฉ์๋ ์์ฑ
- ์์น : com.pcy.dayonetest.service (test)
@Test
public void savePassedStudentScoreTest() {
// given
StudentScore studentScore = StudentScoreTestDataBuilder.passed()
.studentName("pcy")
.build();
// when
studentScoreService.saveScore(
studentScore.getStudentName(),
studentScore.getExam(),
studentScore.getKorScore(),
studentScore.getEnglishScore(),
studentScore.getMathScore()
);
entityManager.flush();
entityManager.clear();
// then
List<ExamPassStudentResponse> responses = studentScoreService.getPassStudentsList(studentScore.getExam());
Assertions.assertEquals(1, responses.size());
ExamPassStudentResponse examPassStudentResponse = responses.get(0);
MyCalculator calculator = new MyCalculator(0.0);
Assertions.assertEquals(studentScore.getStudentName(), examPassStudentResponse.getStudentName());
Assertions.assertEquals(
calculator
.add(studentScore.getMathScore().doubleValue())
.add(studentScore.getKorScore().doubleValue())
.add(studentScore.getEnglishScore().doubleValue())
.divide(3.0)
.getResult(),
examPassStudentResponse.getAvgScore()
);
}
1. ์ค๋น ๋จ๊ณ (Given)
• StudentScore ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. ์ด ๊ฐ์ฒด๋ StudentScoreTestDataBuilder.passed()๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ๋ฉ๋๋ค.
passed()๋ ํฉ๊ฒฉํ ํ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ์ค์ ํด๋๊ณ , studentName("pcy")๋ฅผ ํธ์ถํ์ฌ ํ์ ์ด๋ฆ์ “pcy”๋ก ์ค์ ํฉ๋๋ค.
๋ง์ง๋ง์ผ๋ก build() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ StudentScore ๊ฐ์ฒด๋ฅผ ๋ง๋ญ๋๋ค.
2. ์คํ ๋จ๊ณ (When):
• studentScoreService.saveScore() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํ์์ ์ ์๋ฅผ ์ ์ฅํฉ๋๋ค.
์ด๋ ํ์์ ์ด๋ฆ, ์ํ ์ด๋ฆ, ๊ตญ์ด ์ ์, ์์ด ์ ์, ์ํ ์ ์๋ฅผ ์ธ์๋ก ์ ๋ฌํฉ๋๋ค.
3. ๊ฒ์ฆ ๋จ๊ณ (Then):
• studentScoreService.getPassStudentsList() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํน์ ์ํ์ ํฉ๊ฒฉํ ํ์ ๋ชฉ๋ก์ ๊ฐ์ ธ์ต๋๋ค.
์ด ๋ชฉ๋ก์ ExamPassStudentResponse ๊ฐ์ฒด์ ๋ฆฌ์คํธ๋ก ๋ฐํ๋ฉ๋๋ค.
Assertions.assertEquals(1, responses.size());
• ๊ฐ์ ธ์จ ๋ฆฌ์คํธ์ ํฌ๊ธฐ๊ฐ 1์ธ์ง ํ์ธํฉ๋๋ค. ์ด๋ ํ ๋ช ์ ํ์์ด ํฉ๊ฒฉํ์์ ์๋ฏธํฉ๋๋ค.
Assertions.assertEquals(
calculator
.add(studentScore.getMathScore().doubleValue())
.add(studentScore.getKorScore().doubleValue())
.add(studentScore.getEnglishScore().doubleValue())
.divide(3.0)
.getResult(),
examPassStudentResponse.getAvgScore()
);
• ๊ณ์ฐํ ํ๊ท ์ ์๊ฐ ExamPassStudentResponse ๊ฐ์ฒด์ ํ๊ท ์ ์์ ๊ฐ์์ง ํ์ธํฉ๋๋ค.
• ํด๋น ํ ์คํธ๋ฅผ ์ํํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ ํ ์ ์์ต๋๋ค.
*********************************************
entityManager.flush()์ entityManager.clear()๋ฅผ ํธ์ถํ๋ ์ด์ ๋ฅผ ์ค๋ช ํด๋๋ฆฌ๊ฒ ์ต๋๋ค.
entityManager.flush()
entityManager.flush()๋ ์์์ฑ ์ปจํ ์คํธ(Persistence Context)์ ์๋ ๋ณ๊ฒฝ ์ฌํญ๋ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ฐ์ ๋ก ๋๊ธฐํํ๋ ์ญํ ์ ํฉ๋๋ค. ์์์ฑ ์ปจํ ์คํธ๋ ์ผ๋ฐ์ ์ผ๋ก ์ํฐํฐ ๋งค๋์ ๊ฐ ๊ด๋ฆฌํ๋ ์บ์๋ก, ์ด๊ณณ์ ์๋ ์ํฐํฐ๋ ์๋์ผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋๊ธฐํ๋์ง ์์ต๋๋ค. flush()๋ฅผ ํธ์ถํ๋ฉด, ๋ค์๊ณผ ๊ฐ์ ํจ๊ณผ๊ฐ ์์ต๋๋ค:
1. ๋ณ๊ฒฝ ์ฌํญ ๋ฐ์: ์์์ฑ ์ปจํ ์คํธ์ ๋ณด๊ด๋ ๋ชจ๋ ๋ณ๊ฒฝ ์ฌํญ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ํฉ๋๋ค. ์ด๋ฅผ ํตํด ํ์ฌ๊น์ง์ ๋ณ๊ฒฝ ๋ด์ฉ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋๋ฉฐ, ํธ๋์ญ์ ์ด ์ข ๋ฃ๋์ง ์์๋ ๋ฐ์ดํฐ๋ฅผ ํ์ ํ ์ ์์ต๋๋ค.
2. ๋ฐ์ดํฐ ์ผ๊ด์ฑ ๋ณด์ฅ: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ์ฌ ์ํ์ ์์์ฑ ์ปจํ ์คํธ์ ์ํ๊ฐ ์ผ์นํ๊ฒ ๋ฉ๋๋ค. ์ด๋ ์ดํ์ ๋ฐ์ดํฐ ์กฐํ๋ ๊ฒ์ฆ ์์ ์์ ์ผ๊ด์ฑ์ ๋ณด์ฅํฉ๋๋ค.
entityManager.clear()
entityManager.clear()๋ ์์์ฑ ์ปจํ ์คํธ๋ฅผ ๋น์๋๋ค. ์ฆ, ์์์ฑ ์ปจํ ์คํธ์ ๋ณด๊ด๋ ๋ชจ๋ ์ํฐํฐ๋ฅผ ์ ๊ฑฐํฉ๋๋ค. ์ด๋ฅผ ํธ์ถํ๋ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
1. ์บ์ ๋ฌดํจํ: ์์์ฑ ์ปจํ ์คํธ์ ๋จ์ ์๋ ์บ์๋ ์ํฐํฐ๋ค์ ์ ๊ฑฐํฉ๋๋ค. ์ด๋ ๊ฒ ํจ์ผ๋ก์จ ์ดํ์ ๋ฐ์ดํฐ ์กฐํ ์์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ง์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ฒ ๋์ด, ์์์ฑ ์ปจํ ์คํธ์ ์บ์๋ ๋ฐ์ดํฐ๊ฐ ์๋ ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ต์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
2. ๋ ๋ฆฝ์ ์ธ ๊ฒ์ฆ: ์ํฐํฐ ๋งค๋์ ์ ์บ์๋ฅผ ๋น์์, ๋ฐ์ดํฐ๊ฐ ์์์ฑ ์ปจํ ์คํธ์ ๋จ์์์ง ์๊ณ ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋์์์ ํ์ธํ ์ ์์ต๋๋ค. ์ด๋ ํ ์คํธ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ํ ๋ฐ๋ก ์กฐํํ ๋, ์บ์๋ ๋ฐ์ดํฐ๊ฐ ์๋ ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ฆํ ์ ์๊ฒ ํฉ๋๋ค.
[์ด ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ์๋๋ฆฌ์ค]
• ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ฑฐ๋ ๋ณ๊ฒฝํ ํ, ๊ทธ ๋ณ๊ฒฝ ์ฌํญ์ด ์ค์ ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์๋์๋์ง ํ์ธํ๊ณ ์ ํ ๋.
• ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ๋, ์์์ฑ ์ปจํ ์คํธ์ ์บ์๊ฐ ์๋ ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ต์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ ํ ๋.
*********************************************
2. Github์ผ๋ก ํ์ธ
์ ์ฒด ํ์ผ ํ์ธ(ํ์ฌ ๋ณ๊ฒฝ๋ด์ญ)
[Github]
์ฐธ์กฐ : ์ธํ๋ฐ ๊ฐ์ [์ฅฌ์ฅฌ์ ํจ๊ป ํ๋ฃจ๋ง์ ๋๋ด๋ ์คํ๋ง ํ ์คํธ]
'๐ป FrameWork(ํ๋ ์์ํฌ) > SpringTEST(์คํ๋งํ ์คํธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
4-1 [ํตํฉTEST] testcontainers ์ฌ์ฉํ๊ธฐ ์ํ ์ค์ ๋ฐ ๊ฐ๋จํ DB TEST (0) | 2024.06.15 |
---|---|
3-6 [Mocktio] ๋ฆฌํฉํ ๋ง ์์ (0) | 2024.06.07 |
3-5 [Mocktio] ArugmentCaptor (๋ฐ์ดํฐ ์ ๋ ฅ ์ธ์ ํ์ธ) (0) | 2024.06.06 |
3-4 [Mocktio] Stubbing ํ ์คํธ(๊ฐ์ง ๋ฐ์ดํฐ ์์ฑ) (0) | 2024.06.06 |
3-3 [Mocktio] ํ์ ๊ฒ์ฆ TEST(๋ฉ์๋ ํธ์ถ ์ฌ๋ถ ํ์ธ) (0) | 2024.06.06 |