11package com .github .valb3r .springbatch .adapters .examples .neo4j ;
22
33import org .junit .jupiter .api .Test ;
4+ import org .springframework .batch .core .Job ;
5+ import org .springframework .batch .core .JobParameters ;
6+ import org .springframework .batch .core .configuration .annotation .JobBuilderFactory ;
7+ import org .springframework .batch .core .configuration .annotation .StepBuilderFactory ;
8+ import org .springframework .batch .core .launch .JobLauncher ;
49import org .springframework .batch .core .repository .dao .JobInstanceDao ;
510import org .springframework .beans .factory .annotation .Autowired ;
611import org .springframework .boot .test .context .SpringBootTest ;
12+ import org .springframework .test .annotation .DirtiesContext ;
713import org .springframework .test .context .ActiveProfiles ;
814
15+ import java .util .concurrent .atomic .AtomicReference ;
16+
917import static org .assertj .core .api .Assertions .assertThat ;
1018
1119@ ActiveProfiles ("test" )
1220@ SpringBootTest
21+ @ DirtiesContext (classMode = DirtiesContext .ClassMode .AFTER_EACH_TEST_METHOD )
1322class SimpleJobServiceTest {
1423
1524 @ Autowired
@@ -18,11 +27,38 @@ class SimpleJobServiceTest {
1827 @ Autowired
1928 private JobInstanceDao instanceDao ;
2029
30+ @ Autowired
31+ private JobLauncher launcher ;
32+
33+ @ Autowired
34+ private JobBuilderFactory jobBuilderFactory ;
35+
36+ @ Autowired
37+ private StepBuilderFactory stepBuilderFactory ;
38+
2139 @ Test
2240 void runSimpleJob () {
2341 simpleJobService .runSimpleJob ();
2442
2543 assertThat (simpleJobService .getResult ()).hasValue ("Step TWO DONE" );
2644 assertThat (instanceDao .getJobNames ()).hasSize (1 );
2745 }
46+
47+ @ Test
48+ void runSimpleJobThroughLauncher () throws Exception {
49+ AtomicReference <String > result = new AtomicReference <>();
50+ Job job = jobBuilderFactory .get ("FOO" )
51+ .start (stepBuilderFactory .get ("ONE" ).tasklet ((a , b ) -> null ).build ())
52+ .next (stepBuilderFactory .get ("TWO" ).tasklet ((a , b ) -> {
53+ result .set ("Step TWO DONE" );
54+ return null ;
55+ }).build ())
56+ .build ();
57+
58+
59+ launcher .run (job , new JobParameters ());
60+
61+ assertThat (result ).hasValue ("Step TWO DONE" );
62+ assertThat (instanceDao .getJobNames ()).hasSize (1 );
63+ }
2864}
0 commit comments