公式Springバッチユニットテストガイド

に従って標準ユニットテストケースを作成します。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/job-abc.xml",
    "classpath:spring/batch/config/context.xml"})

public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    }
}

__P.S `spring-batch-test.jar`がクラスパスに追加されました。

問題

ユニットテストの上で起動すると、 `JobLauncherTestUtils`にそのようなBeanエラーメッセージが表示されませんか?

org.springframework.beans.factory.BeanCreationException: Could not autowire field:
    private org.springframework.batch.test.JobLauncherTestUtils com.mkyong.AppTest.jobLauncherTestUtils;
    ......
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
   [org.springframework.batch.test.JobLauncherTestUtils]found for dependency:
    expected at least 1 bean which qualifies as autowire candidate for this dependency.
    ......
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
    ...

解決策


spring-batch-test.jar`をクラスパスに追加すると自動的に

JobLauncherTestUtils`ビーンが作成されません。

これを修正するには、Springのコンフィグレーションファイルの1つで `JobLauncherTestUtils`ビーンを宣言します。

spring/batch/config/test-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean class="org.springframework.batch.test.JobLauncherTestUtils"/>

</beans>

ユニットテストにロードします。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/job-abc.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    }
}

参考文献

  1. link://spring-batch/spring-batch-unit-test-example/[Springバッチユニット

バッチユニットテスト公式ガイド]