如何为JSR-352样式的Spring Batch Job编写测试用例

时间:2016-03-06 17:09:23

标签: java spring unit-testing spring-batch

我使用JsrJobOperator配置并运行了一个Spring批处理作业。有一个META-INF / batch.xml文件定义了批处理工件,我的工作在META-INF / batch-jobs文件夹中的XML文件中定义(参见此处的Archive-loader)。它工作正常,现在我想为此编写一个单元测试。

我尝试使用JobLauncherTestUtils。但看起来它无法找到我的工作。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:618)
... 63 more

这是我的测试程序:

@RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = {
    "classpath:META-INF/batch.xml",
    "classpath:test-context.xml"})
  public class MyUnitTest {


@Autowired
JobLauncherTestUtils jobLauncherTestUtils;


@Test
public void testSetup() throws Exception {
    JobExecution jobExecution =  jobLauncherTestUtils.launchJob();
    Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);
}

}

我的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">
    <!--<property name="job" value="ACER"/> => If I uncomment this, I get Failed to convert property value of type [java.lang.String] to required type [org.springframework.batch.core.Job], maybe there is a way to refer to the Job somehow?-->
</bean>

  <bean id="transactionManager"
      class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />


<bean id="jobLauncher"
      class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>


<import resource="database.xml" />

<bean id="jobOperator"
      class="org.springframework.batch.core.launch.support.SimpleJobOperator"/>
<bean id="jobExplorer"
      class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
</bean>

<bean id="jobRegistry"
      class="org.springframework.batch.core.configuration.support.MapJobRegistry" />

<bean id="jobRepository"
      class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseType" value="mysql" />
</bean>
</beans>

问题:如何为JSR-352样式批处理作业编写junits,我是否正在尝试使用JobLauncherTestUtils?

0 个答案:

没有答案