Issue
I have recently start doing groovy testing in order to test a spring batch job that is using one step (ItemReader, ItemProcessor, ItemWriter)
Knowing that I am using h2 memory database, I have tried to launch the job also the step via the unit test, I am unable to verify that job has been completed:
def 'test launch job'() {
given:
def projectId = 1234L
def fileType = FileType.TEST
def sourceId = 1234506L
def enteredBy = 'groovy_unit_test'
def prePlanYear = 2022L
when:
JobParametersBuilder builder = new JobParametersBuilder();
builder.addLong("projectId", projectId)
.addString("fileType", fileType.toString())
.addLong("sourceId", sourceId)
.addString("enteredBy", enteredBy)
.addLong("prePlanYear", prePlanYear)
//JobExecution jobExecution = jobLauncher.launchJob( builder.toJobParameters())
JobExecution jobExecution = jobLauncher.launchStep(JobConstants.StepNames.xwalkMappingStep.name(), builder.toJobParameters())
then: 'Job completes with no errors.'
jobExecution.status == BatchStatus.COMPLETED
//jobExecution.exitStatus == ExitStatus.COMPLETED
}
the status of the job always remains STARTED:
Also, When I tried to debug it does not go from end to end, it stop in the ItemReader once the test has finished. I see that it doesn't wait for the job to be completed.
Is there a Way to complete the test only after the job has finished?
@PropertySource('classpath:application.properties')
@ActiveProfiles('test')
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = ["spring.h2.console.enabled=true"])
@ImportAutoConfiguration([ FeignAutoConfiguration.class])
@Import([TestConfig.class])
class BaseSpec extends Specification{
@PersistenceContext
EntityManager entityManager
@Autowired
JobRepository jobRepository
@Autowired
DataSource dataSource
@Autowired
JpaTransactionManager jpaTransactionManager
@Autowired
JobLauncherTestUtils jobLauncher
}
@Configuration
@EnableBatchProcessing
class TestConfig {
}
Thank you
Solution
That's because you did not run the entire job, but only its step here:
JobExecution jobExecution = jobLauncher.launchStep(JobConstants.StepNames.xwalkMappingStep.name(), builder.toJobParameters())
You should run the job with JobLauncher#launchJob
and not only the step.
Answered By - Mahmoud Ben Hassine
Answer Checked By - Gilberto Lyons (JavaFixing Admin)