[Testing] JobScopeTestExecutionListener overrides own JobExecution with id 123 #3881
Labels
has: minimal-example
Bug reports that provide a minimal complete reproducible example
in: test
status: feedback-provided
Issues for which the feedback requested from the reporter was provided
type: bug
Uh oh!
There was an error while loading. Please reload this page.
Bug description
JobScopeTestExecutionListener
registers aJobExecution
with id 123 (created viaMetaDataInstanceFactory#createJobExecution()
) for every test usingJobSynchronizationManager.register()
.If the
Job
under test relies on Job-scoped parameters obtained from the execution stored in theJobSynchronizationManager
, the 123rd job execution will fail, because when the Job starts and attempts to register its ownJobExecution
with id 123, theJobSynchronizationManager
will not store it (since an execution with that id already exists).In my specific case, this leads to expressions like
#{jobParameters['myParam']}
evaluating tonull
, becauseJobScope.getContext()
will return the execution registered by theJobScopeTestExecutionListener
.I spent quite some time debugging this issue, because this only affects code that accesses the
JobExecution
via the synchronization manager and only after exactly 123 times.Environment
Spring Batch 4.3.2, Spring Boot 2.4.4.
Steps to reproduce
Job
with a@JobScope
dStep
that queries the job parameters.Expected behavior
The default id of a
JobExecution
(defined inMetaDataInstanceFactory#DEFAULT_JOB_EXECUTION_ID
) should be a value that is not reached naturally by repeatedly calling a job. The same probably applies for instance ids and step execution ids.Minimal Complete Reproducible example
I attached a demo project
demo.zip
Unzip it and run
./mvnw test
.The project contains a job scoped tasklet step that throws an exception if the job parameter is
null
.A test runs this job, supplying the parameter, and repeats itself 125 times. When running the test, the repetition with execution id 123 fails.
Workaround
Place
somewhere in the test class. This causes
JobScopeTestExecutionListener
to not register its ownJobExecution
.The text was updated successfully, but these errors were encountered: