2016-12-07 101 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,並通過您的建議調整測試用例。我嘗試執行一個不需要作業參數的步驟,觀察結果是,整個作業開始並查找作業參數,並在作業查找作業參數....時出現故障......這是預期的行爲... 。 –