2017-02-15 21 views
0

我有一個運行兩個步驟的單個作業的spring批處理應用程序。我希望能夠單獨測試每一步,而無需運行其他步驟。這可能嗎?我的代碼如下:如何測試Spring Batch中的單個步驟而不運行其他?

@Bean 
public Job job() throws Exception { 
    return jobs.get("job") 
      .incrementer(new RunIdIncrementer()) 
      .listener(new JobCompletionNotificationListener()) 
      .start(A) 
      .next(B) 
      .build(); 
} 


@Test 
public void testStepA() { 
    JobExecution execution = launcher.launchStep("A"); 
    assertEquals(BatchStatus.COMPLETED, execution.getStatus()); 
} 

但是,當我運行上面的測試它本質上啓動並運行我的整個工作從前到後。

回答

0

假設您正在運行JUnit測試用例,可以使用JobLauncherTestUtils啓動特定步驟。您還需要通過MetaDataInstanceFactory創建一個ExecutionContext實例,該實例傳遞給JobLauncherTestUtils.launchStep()方法。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"classpath:/batch/test-job.xml" }) 
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class }) 
public class StepTest { 
    @Autowired 
    private JobLauncherTestUtils jobLauncherTestUtils; 
    private JobExecution jobExecution; 
    public ExecutionContext getExecutionContext() { 
     return MetaDataInstanceFactory.createJobExecution().getExecutionContext(); 
    } 

    @Test 
    @DirtiesContext 
    public void testStep() { 
     ExecutionContext executionContext = getExecutionContext(); 
     jobExecution = jobLauncherTestUtils.launchStep(<STEP_NAME>, <JOB_PARAMS>, executionContext); 
    } 
} 

希望幫助!

相關問題