2016-12-07 78 views
0

我试图测试Spring Batch步骤。我有2个方案来测试 1。步骤与微进程(步骤作用域) 2步骤与ItemReader/ItemWriter(步骤范围)Spring-Batch:测试步骤作用域步骤

我的测试类注解如下

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class}) 
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ItemReturnsApplication.class) 

This is how my test class looks like 

@Bean 
    JobLauncherTestUtils jobLauncherTestUtils(){ 
     return new JobLauncherTestUtils(); 
    } 

    @Autowired 
    private JobLauncherTestUtils jobLauncherTestUtils; 

@Test 
    public void testLaunchJob() throws Exception{ 
     JobExecution jobExecution = jobLauncherTestUtils.launchJob(
       new JobParametersBuilder().addString("chunkSize", "3").addString("current_date","2016-11-25").toJobParameters() 
     ); 
     commonAssertions(jobLauncherTestUtils.launchJob()); 
     assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); 
    } 

当运行测试用例,因为作业参数没有传递到我的工作中,所以进程失败。

我正在寻找测试Spring批次中的步骤范围步骤的正确方法。

感谢, 开源浏览器

回答

0

您当前的代码试图启动和测试一份工作,而不是一步。根据如何测试的各个步骤的弹簧批次documentation,如何测试一个tasklet和注入上下文到微进程一个简单的例子是更符合下面的代码行:

@ContextConfiguration 
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 
    StepScopeTestExecutionListener.class }) 
@RunWith(SpringJUnit4ClassRunner.class) 
public class StepScopeTestExecutionListenerIntegrationTests { 

    // This component is defined step-scoped, so it cannot be injected unless 
    // a step is active... 
    @Autowired 
    private YourTaskletClass yourTaskletClass; 

    public StepExecution getStepExection() { 
     StepExecution execution = MetaDataInstanceFactory.createStepExecution(); 
     execution.getExecutionContext().putString("input.data", "foo,bar,spam"); 
     return execution; 
    } 

    @Test 
    public void testYourTaskletClass() { 
     // The tasklet is initialized and some configuration is already set by the MetaDatAInstanceFactory   
     assertNotNull(yourTaskletClass.doSomething()); 
    } 

} 

@RunWith(SpringJUnit4ClassRunner.class)注释是唯一可能的1.4以上的弹簧启动版本。欲了解更多信息,请参阅this博客。

要启动一个单独的一步尝试调整你的代码:

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class}) 
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ItemReturnsApplication.class)  
public class StepIntegrationTest { 

    @Autowired 
    private JobLauncherTestUtils jobLauncherTestUtils; 

    @Test 
    public void testLaunchJob() throws Exception{ 
     JobExecution jobExecution = jobLauncherTestUtils.launchStep("nameOfYourStep"); 

    } 
} 
+0

嗨桑德,我在我的春天启动更新到1.4.1,并通过您的建议调整测试用例。我尝试执行一个不需要作业参数的步骤,观察结果是,整个作业开始并查找作业参数,并在作业查找作业参数....时出现故障......这是预期的行为... 。 –