본문 바로가기
공부/프로그래밍

[springboot] Spring batch 테스트 시 사용하는 JobLauncherTestUtils 쓸 때 설정법

by demonic_ 2019. 3. 29.
반응형

결론 
Test 패키지에 @EnableBatchProcessing 이 포함된 Bean을 생성해야 한다.

 

 

작성된 테스트 클래스

@RunWith(SpringRunner.class)
@SpringBootTest
public class BatchTests {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void 배치테스트(){
        try {
            JobParameters jobParameters = new JobParametersBuilder()
                    .addString("fromDateTime", CommFunc.nowTimeString())
                    .toJobParameters();
            JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);


            System.out.println(jobExecution.getId());
            assertNotNull(jobExecution.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

실행해보면 다음과같은 문구를 보면서 실행이 안되는걸 알 수 있다.

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.CglibAutoProxyConfiguration matched:
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
.
.
.

처음엔 무슨에러인가 해서 한참 찾아봤는데 원하는 대답은 안나오고 그나마 외국사이트에서 다음과 같이 해보라 해서 알게되었다. 다음 파일을 만든다.

java.test.[패키지명] 아래 생성

@EnableBatchProcessing
@Configuration
public class TestJobConfiguration {
    @Bean
    public JobLauncherTestUtils jobLauncherTestUtils() {
        return new JobLauncherTestUtils();
    }
}

이제 테스트를 실행하면 다음과 같이 정상으로 종료된다.

 

끝.

반응형

댓글