Skip to content

Commit a785c07

Browse files
committed
Update testing.adoc
This commit adds the text that was inadvertently removed in efdce56
1 parent 5c57ed4 commit a785c07

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

spring-batch-docs/asciidoc/testing.adoc

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ JUnit facilities
3030
* `@ContextConfiguration(...)`: Indicates which resources to configure the
3131
`ApplicationContext` with.
3232

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+
3337
[role="javaContent"]
34-
The following Java example shows the two annotations in use:
38+
The following Java example shows the annotations in use:
3539

3640
.Using Java Configuration
3741
[source, java, role="javaContent"]
@@ -43,7 +47,7 @@ public class SkipSampleFunctionalTests { ... }
4347
----
4448

4549
[role="xmlContent"]
46-
The following XML example shows the two annotations in use:
50+
The following XML example shows the annotations in use:
4751

4852
.Using XML Configuration
4953
[source, java, role="xmlContent"]
@@ -216,6 +220,38 @@ test method, as if that execution were active in a `Step` at runtime. The factor
216220
is detected by its signature (it must return a `StepExecution`). If a factory method is
217221
not provided, then a default `StepExecution` is created.
218222

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+
219255
The listener approach is convenient if you want the duration of the step scope to be the
220256
execution of the test method. For a more flexible but more invasive approach, you can use
221257
the `StepScopeTestUtils`. The following example counts the number of items available in

0 commit comments

Comments
 (0)