How to instantiate "Step" scope bean of spring in Cucumber Testing?
SpringJUnit4ClassRunner uses @TestExecutionListeners to instantiate the step scoped beans for testing purpose.
I am trying get this behavior in Cucumber. Cucumber uses a @RunWith(Cucumber.class)
Is there anyway we can instantiate step scope bean?
Thanks in advance
I'm not familiar with Cucumber, but I have instantiated/tested step scope items using
@RunWith(SpringJUnit4ClassRunner.class)
I would recommend including the
StepScopeTestExecutionListener.class
as well as theDependencyInjectionTestExecutionListener
(if you're injecting any dependencies) in your@TestExecutionListeners
annotation, e.g.@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
In order to instantiate a step scope bean in the test class, first get an instance of the
ExecutionContext
by utilizing theMetaDataInstanceFactory
.For example:
Once you can have an instance of the
ExecutionContext
, you'll need to make use of theJobLauncherTestUtils
class (documentation: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/test/JobLauncherTestUtils.html). You can launch a step by callinglaunchStep()
, for example:Hope that helps!