Spring Batch Test
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
gradle
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.batch:spring-batch-test'
}
์คํ๋ง ๋ถํธ ๋ฐฐ์น ํ
์คํธ ์ฌ์ฉ์ ์์กด์ฑ์ ๋ฐ๋์ spring-boot-starter-test
๊ฐ ํฌํจ๋์ด ์์ด์ผํ๋ค.
@RunWith(SpringRunner.class)
@SpringBatchTest
@SpringBootTest(classes={BatchJpaTestConfiguration.class, TestBatchConfig.class})
public class BatchIntegrationTestJobConfigurationTest {
@Autowired
@SpringBatchTest
: ์๋์ผ๋ก ApplicatonContext์ ํ
์คํธ์ ํ์ํ ์ฌ๋ฌ ์ ํธ Bean์ ๋ฑ๋กํด์ค๋ค.
JobLauncherTestUtils
์คํ๋ง ๋ฐฐ์น ํ ์คํธ์ ํ์ํ ์ ๋ฐ์ ์ธ ์ ํธ ๊ธฐ๋ฅ๋ค ์ง์
ํตํฉ ํ ์คํธ
JobRepositoryTestUtils
@SpringBootTest
: ํตํฉ ํ
์คํธ ์คํ์ ์ฌ์ฉํ Java์ค์ ๋ค์ ์ ํ
BatchJpaTestConfiguration
์ ํ
์คํธํ Batch Job
TestBatchConfig
: ๋ฐฐ์น ํ
์คํธ ํ๊ฒฝ
jobLauncherTestUtils.launchJob(jobParameters)
: JobParameters
์ ํจ๊ป Job์ ์คํํ๋ค.
ํด๋น Job์ ๊ฒฐ๊ณผ๋ JobExecution
์ ๋ด๊ฒจ ๋ฐํ๋๋ค.
jobExecution.getStatus()
@Configuration
@EnableAutoConfiguration
@EnableBatchProcessing // ๋ฐฐ์น ํ๊ฒฝ์ ์๋ ์ค์
public class TestBatchConfig {
}
@EnableBatchProcessing
: ์คํ๋ง๋ถํธ ๋ฐฐ์น ์คํํฐ์ ๋ฏธ๋ฆฌ ์ ์๋ ์ค์ ๋ค์ ์คํ์ํค๋ ์ด๋
ธํ
์ด์
์ผ๋ก JobBuilder, StepBuilder ๋ฑ ๋ค์ํ ์ค์ ์ฃผ์
DB์ ์์ฑ๋ JobExecution์ ์ฝ๊ฒ ์์ฑ/์ญ์ ๊ฐ๋ฅํ๋๋ก ์ง์
ํตํฉ ํ ์คํธ
StepScopeTestExecutionListener
๋ฐฐ์น ๋จ์ ํ
์คํธ์ @StepScope
์ปจํ
์คํธ๋ฅผ ์์ฑ
ํด๋น ์ปจํ
์คํธ๋ฅผ ํตํด JobParameter๋ฑ์ ๋จ์ ํ
์คํธ์์ DI ๋ฐ์ ์ ์๋ค.
๋จ์ ํ ์คํธ
JobScopeTestExecutionListener
๋ฐฐ์น ๋จ์ ํ
์คํธ์ @JobScope
์ปจํ
์คํธ๋ฅผ ์์ฑ
ํด๋น ์ปจํ
์คํธ๋ฅผ ํตํด JobParameter๋ฑ์ ๋จ์ ํ
์คํธ์์ DI ๋ฐ์ ์ ์๋ค.
๋จ์ ํ ์คํธ