I\'m having Standalone spring batch job. This works perfectly fine when in JUNIT
@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(cl
Michael's comment is working for me, I am also providing JavaConfig copy-paste alternative for lazy people like me :)
@Bean
public StepScope stepScope() {
final StepScope stepScope = new StepScope();
stepScope.setAutoProxy(true);
return stepScope;
}
You can solve by 2 Ways:
A.
Use spring.main.allow-bean-definition-overriding=true
in application.properties
B.
To solve spring boot batch scope issue and avoid the use of spring.main.allow-bean-definition-overriding=true
:
Disable autoProxy
of StepScope. To perform that use following code snippet as a reference:
<bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
<property name="autoProxy" value="false"/>
</bean>
Now perform following Spring bean configuration:
To configure step scope via XML-Based configuration:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="step">
<aop:scoped-proxy/>
</bean>
To configure step scope via Java-Based configuration:
@StepScope
@Component(value = "userPreferences")
public class UserPreference {}
Don't forget to configure
spring.main.allow-bean-definition-overriding=true
Simply adding @SpringBatchTest
on your test class should works.
This may be a bug (we're still investigating), however we do have a work around. The cause of this is that when using @EnableBatchProcessing
the StepScope
that is automatically configured assumes java config and therefore does not proxy the step scoped beans, causing them to be created too soon. The work around is to manually configure a StepScope
in your XML configuration with the following configuration:
<bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
<property name="autoProxy" value="true"/>
</bean>
Seeing as you are using @RunWith(SpringRunner.class)
, declaring @TestExecutionListeners({..., StepScopeTestExecutionListener.class})
above your class will setup the scopes for you.
Same with @TestExecutionListeners({..., JobScopeTestExecutionListener.class})
for jobScope.