Spring Batch Test

ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ

  • 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
    private JobLauncherTestUtils jobLauncherTestUtils; // (2)

    @Autowired
    private SalesRepository salesRepository;

    @Autowired
    private SalesSumRepository salesSumRepository;

    @After
    public void tearDown() throws Exception {
        salesRepository.deleteAllInBatch();
        salesSumRepository.deleteAllInBatch();
    }

    @Test
    public void ๊ธฐ๊ฐ„๋‚ด_Sales๊ฐ€_์ง‘๊ณ„๋˜์–ด_SalesSum์ด๋œ๋‹ค() throws Exception {
        //given
        LocalDate orderDate = LocalDate.of(2019,10,6);
        int amount1 = 1000;
        int amount2 = 500;
        int amount3 = 100;

        salesRepository.save(new Sales(orderDate, amount1, "1"));
        salesRepository.save(new Sales(orderDate, amount2, "2"));
        salesRepository.save(new Sales(orderDate, amount3, "3"));

        JobParameters jobParameters = new JobParametersBuilder() 
                .addString("orderDate", orderDate.format(FORMATTER))
                .toJobParameters();

        //when
        JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters); // (3)


        //then
        assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
        List<SalesSum> salesSumList = salesSumRepository.findAll();
        assertThat(salesSumList.size()).isEqualTo(1);
        assertThat(salesSumList.get(0).getOrderDate()).isEqualTo(orderDate);
        assertThat(salesSumList.get(0).getAmountSum()).isEqualTo(amount1+amount2+amount3);
    }
}
  • @SpringBatchTest : ์ž๋™์œผ๋กœ ApplicatonContext์— ํ…Œ์ŠคํŠธ์— ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ ์œ ํ‹ธ Bean์„ ๋“ฑ๋กํ•ด์ค€๋‹ค.

    Util
    ์„ค๋ช…
    ํ…Œ์ŠคํŠธ ๋‹จ์œ„

    JobLauncherTestUtils

    ์Šคํ”„๋ง ๋ฐฐ์น˜ ํ…Œ์ŠคํŠธ์— ํ•„์š”ํ•œ ์ „๋ฐ˜์ ์ธ ์œ ํ‹ธ ๊ธฐ๋Šฅ๋“ค ์ง€์›

    ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ

    JobRepositoryTestUtils

    DB์— ์ƒ์„ฑ๋œ JobExecution์„ ์‰ฝ๊ฒŒ ์ƒ์„ฑ/์‚ญ์ œ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ง€์›

    ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ

    StepScopeTestExecutionListener

    ๋ฐฐ์น˜ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์‹œ @StepScope ์ปจํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑ ํ•ด๋‹น ์ปจํ…์ŠคํŠธ๋ฅผ ํ†ตํ•ด JobParameter๋“ฑ์„ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์—์„œ DI ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

    ๋‹จ์œ„ ํ…Œ์ŠคํŠธ

    JobScopeTestExecutionListener

    ๋ฐฐ์น˜ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์‹œ @JobScope ์ปจํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑ ํ•ด๋‹น ์ปจํ…์ŠคํŠธ๋ฅผ ํ†ตํ•ด JobParameter๋“ฑ์„ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ์—์„œ DI ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

    ๋‹จ์œ„ ํ…Œ์ŠคํŠธ

  • @SpringBootTest : ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์‹คํ–‰์‹œ ์‚ฌ์šฉํ•  Java์„ค์ •๋“ค์„ ์„ ํƒ

    • BatchJpaTestConfiguration์€ ํ…Œ์ŠคํŠธํ•  Batch Job

    • TestBatchConfig : ๋ฐฐ์น˜ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ

  • jobLauncherTestUtils.launchJob(jobParameters) : JobParameters์™€ ํ•จ๊ป˜ Job์„ ์‹คํ–‰ํ•œ๋‹ค.

    • ํ•ด๋‹น Job์˜ ๊ฒฐ๊ณผ๋Š” JobExecution์— ๋‹ด๊ฒจ ๋ฐ˜ํ™˜๋œ๋‹ค.

    • jobExecution.getStatus()๋กœ ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค.

@Configuration
@EnableAutoConfiguration
@EnableBatchProcessing // ๋ฐฐ์น˜ ํ™˜๊ฒฝ์„ ์ž๋™ ์„ค์ •
public class TestBatchConfig {
}
  • @EnableBatchProcessing : ์Šคํ”„๋ง๋ถ€ํŠธ ๋ฐฐ์น˜ ์Šคํƒ€ํ„ฐ์— ๋ฏธ๋ฆฌ ์ •์˜๋œ ์„ค์ •๋“ค์„ ์‹คํ–‰์‹œํ‚ค๋Š” ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ JobBuilder, StepBuilder ๋“ฑ ๋‹ค์–‘ํ•œ ์„ค์ • ์ฃผ์ž…

์ฐธ๊ณ 

Last updated