@@ -30,8 +30,12 @@ JUnit facilities
30
30
* `@ContextConfiguration(...)`: Indicates which resources to configure the
31
31
`ApplicationContext` with.
32
32
33
+ Starting from v4.1, it is also possible to inject Spring Batch test utilities
34
+ like the `JobLauncherTestUtils` and `JobRepositoryTestUtils` in the test context
35
+ using the `@SpringBatchTest` annotation.
36
+
33
37
[role="javaContent"]
34
- The following Java example shows the two annotations in use:
38
+ The following Java example shows the annotations in use:
35
39
36
40
.Using Java Configuration
37
41
[source, java, role="javaContent"]
@@ -43,7 +47,7 @@ public class SkipSampleFunctionalTests { ... }
43
47
----
44
48
45
49
[role="xmlContent"]
46
- The following XML example shows the two annotations in use:
50
+ The following XML example shows the annotations in use:
47
51
48
52
.Using XML Configuration
49
53
[source, java, role="xmlContent"]
@@ -216,6 +220,38 @@ test method, as if that execution were active in a `Step` at runtime. The factor
216
220
is detected by its signature (it must return a `StepExecution`). If a factory method is
217
221
not provided, then a default `StepExecution` is created.
218
222
223
+ Starting from v4.1, the `StepScopeTestExecutionListener` and
224
+ `JobScopeTestExecutionListener` are imported as test execution listeners
225
+ if the test class is annotated with `@SpringBatchTest`. The preceding test
226
+ example can be configured as follows:
227
+
228
+ [source, java]
229
+ ----
230
+ @SpringBatchTest
231
+ @RunWith(SpringRunner.class)
232
+ @ContextConfiguration
233
+ public class StepScopeTestExecutionListenerIntegrationTests {
234
+
235
+ // This component is defined step-scoped, so it cannot be injected unless
236
+ // a step is active...
237
+ @Autowired
238
+ private ItemReader<String> reader;
239
+
240
+ public StepExecution getStepExecution() {
241
+ StepExecution execution = MetaDataInstanceFactory.createStepExecution();
242
+ execution.getExecutionContext().putString("input.data", "foo,bar,spam");
243
+ return execution;
244
+ }
245
+
246
+ @Test
247
+ public void testReader() {
248
+ // The reader is initialized and bound to the input data
249
+ assertNotNull(reader.read());
250
+ }
251
+
252
+ }
253
+ ----
254
+
219
255
The listener approach is convenient if you want the duration of the step scope to be the
220
256
execution of the test method. For a more flexible but more invasive approach, you can use
221
257
the `StepScopeTestUtils`. The following example counts the number of items available in
0 commit comments