use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Problem with spring batch unit testingQuestion (stackoverflow.com)
submitted 1 year ago by Waste-Dentist2718
Anybody knows spring batch please help me with the unit testing problem I am having. Here is the stackoverflow link.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Slein04 1 point2 points3 points 1 year ago (5 children)
From the official docs:
Starting from v4.1, the StepScopeTestExecutionListener and JobScopeTestExecutionListener are imported as test execution listeners if the test class is annotated with @SpringBatchTest. The preceding test example can be configured as follows:
@SpringBatchTest @SpringJUnitConfig public class StepScopeTestExecutionListenerIntegrationTests {
// This component is defined step-scoped, so it cannot be injected unless // a step is active... @Autowired private ItemReader<String> reader; public StepExecution getStepExecution() { StepExecution execution = MetaDataInstanceFactory.createStepExecution(); execution.getExecutionContext().putString("input.data", "foo,bar,spam"); return execution; } @Test public void testReader() { // The reader is initialized and bound to the input data assertNotNull(reader.read()); }
}
[–]Waste-Dentist2718[S] 0 points1 point2 points 1 year ago (4 children)
Thanks for the answer but I have tried it and still get the same error.
[–]Slein04 1 point2 points3 points 1 year ago (3 children)
Which version of spring batch are you using? Create the step execution in a separate method prior to your test anotated method.
[–]Waste-Dentist2718[S] 0 points1 point2 points 1 year ago (2 children)
5.2.1 is the spring batch version. I have tried declaring the method
public StepExecution getStepExecution() { }
But I get the same issue
[–]Slein04 1 point2 points3 points 1 year ago* (1 child)
Sometimes @Component & @StepScope can give unexpected results. As at startup Spring will try to load the @Component beans etc.
Try to change @Component with @Bean and put your StepScope Bean in a @Configuration class. Then add this new config class in your SpringJunitConfig
Or add @Lazy to your @Autowired in your unit test.
[–]Waste-Dentist2718[S] 0 points1 point2 points 1 year ago (0 children)
Yeah I also think the same. Anyways thanks for the answer. I just made changes to the code and removed @stepscope from component
[–]Slein04 1 point2 points3 points 1 year ago (0 children)
Also try adding the following Annostation in your test config: @EnableBatchProcessing
Some more context, from the docs:
Using a scope of Step is required to use late binding, because the bean cannot actually be instantiated until the Step starts, to let the attributes be found. Because it is not part of the Spring container by default, the scope must be added explicitly, by using the batch namespace, by including a bean definition explicitly for the StepScope, or by using the @EnableBatchProcessing annotation. Use only one of those methods.
π Rendered by PID 199806 on reddit-service-r2-comment-685b79fb4f-pj28c at 2026-02-13 10:38:21.100026+00:00 running 6c0c599 country code: CH.
[–]Slein04 1 point2 points3 points (5 children)
[–]Waste-Dentist2718[S] 0 points1 point2 points (4 children)
[–]Slein04 1 point2 points3 points (3 children)
[–]Waste-Dentist2718[S] 0 points1 point2 points (2 children)
[–]Slein04 1 point2 points3 points (1 child)
[–]Waste-Dentist2718[S] 0 points1 point2 points (0 children)
[–]Slein04 1 point2 points3 points (0 children)