본문 바로가기
Spring|Spring-boot/Spring Batch

Spring Batch Test Code

by oncerun 2023. 5. 28.
반응형

 

Spring Batch를 테스트를 하는 방법은 버전 별로 구성하는 환경이 다른 것 같지만 4.1 버전 이상을 사용할 것이기 때문에 다음과 같이 구성한다.

 

testImplementation 'org.springframework.batch:spring-batch-test'

4.1 버전 이상에서는 @SpringBatchTest 어노테이션으로 자동 스캔을 통해 batch test의 필수 빈을 의존성 주입해 주는 것이다. 

 

실제  @SpringBatchTest 어노테이션 클래스에서 import문만 봐도 빠르게 이해가 될 것이다.

import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.JobScopeTestExecutionListener;
import org.springframework.batch.test.StepScopeTestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension;

 

 

 

Junit4 , 5에 대한 사용법에 차이가 있으니 다음 코드를 확인하고 사용하자.

 

A typical usage of this annotation with JUnit 4 is like:
  @RunWith(SpringRunner.class)
  @SpringBatchTest
  @ContextConfiguration(classes = MyBatchJobConfiguration.class)
  public class MyBatchJobTests {
 
     @Autowired
     private JobLauncherTestUtils jobLauncherTestUtils;
 
     @Autowired
     private JobRepositoryTestUtils jobRepositoryTestUtils;
 
     @Before
     public void clearJobExecutions() {
        this.jobRepositoryTestUtils.removeJobExecutions();
     }
 
     @Test
     public void testMyJob() throws Exception {
        // given
        JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
 
        // when
        JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
 
        // then
        Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
     }
 
  }
  
For JUnit 5, this annotation can be used without having to manually register the SpringExtension since @SpringBatchTest is meta-annotated with @ExtendWith(SpringExtension.class):
  @SpringBatchTest
  @ContextConfiguration(classes = MyBatchJobConfiguration.class)
  public class MyBatchJobTests {
 
     @Autowired
     private JobLauncherTestUtils jobLauncherTestUtils;
 
     @Autowired
     private JobRepositoryTestUtils jobRepositoryTestUtils;
 
     @BeforeEach
     public void clearJobExecutions() {
        this.jobRepositoryTestUtils.removeJobExecutions();
     }
 
     @Test
     public void testMyJob() throws Exception {
        // given
        JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
 
        // when
        JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
 
        // then
        Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
     }
  
  }

 

 

 

JobLauncherTestUtils는 테스트를 위한 다양한 기능을 제공한다. 예를들면 Job을 실행할 수도 혹은 특정 Step만을 실행할 수 도 있다. 

 

다만 BatchProcessing을 위해선 별도의 클래스를 다음과 같이 만들어주고 테스트 컨텍스트에 사용한다고 선언해주어야 한다.

 

@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class TestConfiguration {

}

 

실제 연습을 위한 배치 테스트 코드는 다음과 같다.

 

https://github.com/sungil-yu/exampleBatch/commit/69b1dd90bf51f8f3d9999a03a0aaca784ed2cb95

 

batchTest · sungil-yu/exampleBatch@69b1dd9

Show file tree Showing 4 changed files with 107 additions and 6 deletions.

github.com

 

반응형

'Spring|Spring-boot > Spring Batch' 카테고리의 다른 글

Batch 성능 개선  (1) 2023.05.29
Batch 예제  (0) 2023.05.24
Spring Batch 아키텍처  (0) 2023.05.21
Spring Batch about 15 minutes  (0) 2023.05.14
Batch란  (1) 2023.05.14

댓글