如何在黄瓜测试中实例化Spring的“Step”范围bean?Spring批处理:如何在黄瓜测试中实例化“Step”作用域bean?
SpringJUnit4ClassRunner使用@TestExecutionListeners来实例化步骤范围的bean以用于测试目的。
我想在黄瓜中得到这种行为。黄瓜使用@RunWith(Cucumber.class)
是否有无论如何我们可以实例化步骤范围bean?
在此先感谢
如何在黄瓜测试中实例化Spring的“Step”范围bean?Spring批处理:如何在黄瓜测试中实例化“Step”作用域bean?
SpringJUnit4ClassRunner使用@TestExecutionListeners来实例化步骤范围的bean以用于测试目的。
我想在黄瓜中得到这种行为。黄瓜使用@RunWith(Cucumber.class)
是否有无论如何我们可以实例化步骤范围bean?
在此先感谢
我不熟悉的黄瓜,但我已经用实例@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>);
希望帮助!
通过探索一些选项,我已经使它现在可行。
下面是我的解决办法
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,可能我会写一个超类,并在超级构造函数中实现。
如果您有任何疑虑,请告诉我。
再次感谢
感谢您的详细信息。我有相同的实现。我只想尝试使用CUCUMBER。我在这个开源产品上可能为时过早。 Listeners功能由Spring runner提供,在CUCUMBER中应该提供相同的功能。 –