2016-12-28 78 views

回答

0

我不熟悉的黄瓜,但我已经用实例@RunWith(SpringJUnit4ClassRunner.class)

我会建议包括StepScopeTestExecutionListener.class还有DependencyInjectionTestExecutionListener /测试步范围的项目(如果你是注入任何依赖)在你的@TestExecutionListeners注解,例如@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })

为了在测试类中实例化一个步骤范围bean,首先通过使用MetaDataInstanceFactory获取ExecutionContext的一个实例。

例如:

ExecutionContext executionContext = MetaDataInstanceFactory.createJobExecution().getExecutionContext(); 

一旦你可以有ExecutionContext的一个实例,你需要利用JobLauncherTestUtils类(文件:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/test/JobLauncherTestUtils.html)的。你可以通过调用launchStep(),例如推出了一步:

jobLauncherTestUtils.launchStep(<step_name>, <job_parameters>, <execution_context>); 

希望帮助!

+0

感谢您的详细信息。我有相同的实现。我只想尝试使用CUCUMBER。我在这个开源产品上可能为时过早。 Listeners功能由Spring runner提供,在CUCUMBER中应该提供相同的功能。 –

0

通过探索一些选项,我已经使它现在可行。

下面是我的解决办法

bean配置

@Bean(name = "abc") 
@StepScope 
public ABC getABC(){ 
return new ABC(); 
} 

@ContextConfiguration(classes = AppConfiguration.class, loader = AnnotationConfigContextLoader.class) 
public class StepDef { 

@Autowire 
ABC abc; 

public StepDef{ 

     StepExecution stepExecution = getStepExecution(); 
     StepSynchronizationManager.register(stepExecution); 

} 

} 

我不知道如何正确的是这个实现。我已经初始化StepExecution在stepDef构造函数中加载我的配置,以便我的AutoWiring可以正常工作,并且可以运行我的测试。

我需要遵循同样的方法,所有stepDef,可能我会写一个超类,并在超级构造函数中实现。

如果您有任何疑虑,请告诉我。

再次感谢