11/*
2- * Copyright 2014-2022 the original author or authors.
2+ * Copyright 2014-present the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package org .springframework .batch .test ;
1717
18- import org .jspecify .annotations .Nullable ;
18+ import javax .sql .DataSource ;
19+
1920import org .junit .jupiter .api .Test ;
2021
2122import org .springframework .batch .core .ExitStatus ;
23+ import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
2224import org .springframework .batch .core .configuration .annotation .EnableJdbcJobRepository ;
2325import org .springframework .batch .core .job .Job ;
2426import org .springframework .batch .core .job .JobExecution ;
25- import org .springframework .batch .core .step .Step ;
26- import org .springframework .batch .core .step .StepContribution ;
27- import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
2827import org .springframework .batch .core .job .builder .JobBuilder ;
29- import org .springframework .batch .core .launch .JobLauncher ;
3028import org .springframework .batch .core .repository .JobRepository ;
31- import org .springframework .batch .core .scope . context . ChunkContext ;
29+ import org .springframework .batch .core .step . Step ;
3230import org .springframework .batch .core .step .builder .StepBuilder ;
3331import org .springframework .batch .core .step .tasklet .Tasklet ;
3432import org .springframework .batch .infrastructure .repeat .RepeatStatus ;
35- import org .springframework .context .ApplicationContext ;
36- import org .springframework .context . annotation .AnnotationConfigApplicationContext ;
33+ import org .springframework .batch . test . context .SpringBatchTest ;
34+ import org .springframework .beans . factory . annotation .Autowired ;
3735import org .springframework .context .annotation .Bean ;
3836import org .springframework .context .annotation .Configuration ;
3937import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
4038import org .springframework .jdbc .support .JdbcTransactionManager ;
4139
42- import javax .sql .DataSource ;
43-
4440import static org .junit .jupiter .api .Assertions .assertEquals ;
4541
4642/**
4743 * @author mminella
4844 * @author Mahmoud Ben Hassine
4945 */
46+ // TODO remove in 6.2 when JobLauncherTestUtils is removed
47+ @ SpringBatchTest
5048class JobLauncherTestUtilsTests {
5149
50+ @ Autowired
51+ private JobLauncherTestUtils testUtils ;
52+
5253 @ Test
53- void testStepExecutionWithJavaConfig () {
54- ApplicationContext context = new AnnotationConfigApplicationContext ( TestJobConfiguration . class );
54+ void testJobExecutionWithDeprecatedLauncher () throws Exception {
55+ JobExecution execution = testUtils . launchJob ( );
5556
56- JobLauncherTestUtils testUtils = context .getBean (JobLauncherTestUtils .class );
57+ assertEquals (ExitStatus .COMPLETED , execution .getExitStatus ());
58+ }
5759
60+ @ Test
61+ void testStepExecutionWithDeprecatedLauncher () {
5862 JobExecution execution = testUtils .launchStep ("step1" );
5963
6064 assertEquals (ExitStatus .COMPLETED , execution .getExitStatus ());
@@ -66,30 +70,14 @@ void testStepExecutionWithJavaConfig() {
6670 static class TestJobConfiguration {
6771
6872 @ Bean
69- public Step step (JobRepository jobRepository ) {
70- return new StepBuilder ("step1" , jobRepository ).tasklet (new Tasklet () {
71-
72- @ Override
73- public @ Nullable RepeatStatus execute (StepContribution contribution , ChunkContext chunkContext )
74- throws Exception {
75- return null ;
76- }
77- }, transactionManager (dataSource ())).build ();
73+ public Step step (JobRepository jobRepository , JdbcTransactionManager transactionManager ) {
74+ Tasklet tasklet = (contribution , chunkContext ) -> RepeatStatus .FINISHED ;
75+ return new StepBuilder ("step1" , jobRepository ).tasklet (tasklet , transactionManager ).build ();
7876 }
7977
8078 @ Bean
81- public Job job (JobRepository jobRepository ) {
82- return new JobBuilder ("job" , jobRepository ).flow (step (jobRepository )).end ().build ();
83- }
84-
85- @ Bean
86- public JobLauncherTestUtils testUtils (Job jobUnderTest , JobRepository jobRepository , JobLauncher jobLauncher ) {
87- JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils ();
88- jobLauncherTestUtils .setJob (jobUnderTest );
89- jobLauncherTestUtils .setJobRepository (jobRepository );
90- jobLauncherTestUtils .setJobLauncher (jobLauncher );
91-
92- return jobLauncherTestUtils ;
79+ public Job job (JobRepository jobRepository , Step step ) {
80+ return new JobBuilder ("job" , jobRepository ).start (step ).build ();
9381 }
9482
9583 @ Bean
0 commit comments