Quick revision
The short answer
afterStep returns an ExitStatus, and returning null means 'keep the current one', which for a step that processed all its items is COMPLETED. The listener must return a status that says what it found, such as new ExitStatus("FAILED_VALIDATION") or ExitStatus.FAILED, and the job flow must then route on that exit code with .on("FAILED_VALIDATION").fail() or .to(errorStep).
What to remember
In this answer, we saw that afterStep reports its verdict only through its return value, and that null means 'keep the current status', which was COMPLETED. The fix is to return a custom ExitStatus such as FAILED_VALIDATION, and to define the job as a flow whose transitions route on that exit code to a failure or a quarantine step. We also separated the exit status, which we control and route on, from the BatchStatus, which the framework controls, and saw why returning a status is more reliable than throwing from afterStep.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
afterStep returns an ExitStatus, and returning null means 'keep the current one', which for a step that processed all its items is COMPLETED. The listener must return a status that says what it found, such as new ExitStatus("FAILED_VALIDATION") or ExitStatus.FAILED, and the job flow must then route on that exit code with .on("FAILED_VALIDATION").fail() or .to(errorStep).
Why this question is asked
This bug is easy to write and hard to notice. The validation logic is correct, the log even says '30 percent invalid', and yet the job dashboard shows a green COMPLETED and the next step loads half-broken data into production. Interviewers like it because the fix requires understanding two things that people mix up: the exit status that a step reports and the batch status that the framework tracks, and how the job flow chooses the next step. In this answer, we will recap the two statuses, reproduce the bug, see why null changes nothing, fix the listener, wire the job flow to act on the result, and look at why throwing an exception from afterStep is not the right tool.
1A Quick Recap: BatchStatus and ExitStatus
Every StepExecution and JobExecution carries two statuses. BatchStatus is the framework's own lifecycle state: STARTING, STARTED, COMPLETED, FAILED, STOPPED, ABANDONED and a few others. Spring Batch sets it itself, and it decides, for example, whether a failed job can be restarted.
ExitStatus is a description of how the execution ended, made of an exit code (a string) and an optional description. Spring Batch fills in a default that matches the BatchStatus, such as COMPLETED or FAILED, but we are free to set our own codes such as FAILED_VALIDATION or COMPLETED_WITH_SKIPS. The job flow, the part of the job definition that decides which step runs next, makes its decisions on the exit code, not on the BatchStatus. So the exit status is our tool for steering the job.
| BatchStatus | ExitStatus | |
|---|---|---|
| Who sets it | Spring Batch | Spring Batch by default, our listener may change it |
| Values | fixed enum: COMPLETED, FAILED, STOPPED... | any string: COMPLETED, FAILED_VALIDATION... |
| Used for | lifecycle and restart decisions | routing in the job flow, reporting |
Changed by afterStep? | no | yes, by the returned value |
2Reproducing the Bug
The processor returns null for invalid records, which tells Spring Batch to filter the item out, so they are counted in the step's filter count. The listener checks the ratio at the end of the step.
Notice that the listener did everything except the one thing that counts. Adding a failure exception after the step has finished only adds text to the execution record. Logging does not change the result either. The only value Spring Batch reads from afterStep is the return value, and here it is null.
3Why Returning null Keeps COMPLETED
When the step finishes its work, Spring Batch has already set its exit status to COMPLETED. It then calls afterStep on each registered listener and combines the returned value with the current one, using ExitStatus.and(...). The rule of and() is simple: the more severe status wins, and null counts as 'no opinion', so the current status is kept.
So null is not 'nothing happened', it is 'keep what you have'. That is a reasonable default for a listener that only logs, and exactly wrong for a listener that is supposed to judge the step. Severity also explains why a custom code works: Spring Batch ranks unknown custom codes as more severe than COMPLETED, so returning FAILED_VALIDATION replaces COMPLETED when the statuses are combined.
4Fix 1: Return the Status the Listener Decided On
The listener should return a status in both branches: its own code when the data is bad, and the existing status otherwise.
Returning ExitStatus.FAILED would also work, but a specific code tells the job flow and the people reading the report what actually went wrong. Notice that the step's BatchStatus is still COMPLETED: the step did its work without crashing. What changes is the exit code, and that is what the next part acts on.
5Fix 2: Let the Exit Code Drive the Job Flow
A job built with start(...).next(...) runs the steps in order and moves on when a step's BatchStatus is COMPLETED, so a custom exit code alone does not stop it. To act on the code, we define the job as a flow with transitions.
- load step finishes
BatchStatusCOMPLETED - afterStep returns a status
FAILED_VALIDATIONwhen 30 percent are invalid - Exit code is FAILED_VALIDATION?Yesgo to quarantine, then fail the jobNocheck the next pattern
- Exit code is FAILED?Yesfail the jobNogo to publish
4 steps in order. Play them, or step through them one at a time.
The transitions are matched against the exit code, most specific pattern first, so FAILED_VALIDATION goes to the quarantine step and everything else that is not FAILED continues to publish. Ending with .fail() gives the job a BatchStatus of FAILED, which is what monitoring and restart logic look at. If we wanted the job to fail immediately without a cleanup step, .on("FAILED_VALIDATION").fail() on the load step is enough.
There is one more trap here. If a step returns an exit code that no transition matches, the flow cannot find a next state and the job fails with an error saying so. The catch-all .on("*") prevents that surprise.
6Why Not Throw an Exception From afterStep?
It is tempting to throw an exception in afterStep to fail the step. The trouble is that afterStep runs after the step's work has been committed, and Spring Batch has historically caught and logged exceptions from this callback rather than treating them as a step failure. The exact behaviour has changed between versions, so check your version before relying on it. Returning an exit status is the documented contract, it works the same everywhere, and it lets the job flow choose what happens next instead of simply crashing.
If the rule should stop processing early, for example as soon as invalid records pass a limit, the right tool is inside the step: a skip limit on a fault-tolerant step, or a processor that throws once a counter passes the threshold. afterStep is for judging the step after it has finished.
7Follow-up questions
Does changing the ExitStatus make the step restartable?
No. Restart is driven by BatchStatus. A step that finished with BatchStatus COMPLETED and a custom exit code is complete as far as Spring Batch is concerned, and a restart of the job would skip it unless allowStartIfComplete(true) is set. If we want the job to be restartable from that point, the flow should end in a FAILED or STOPPED state, for example with .fail() or .stopAndRestart(step).
Where do I see the exit status after the run?
It is stored in the job repository tables, in the EXIT_CODE and EXIT_MESSAGE columns of BATCH_STEP_EXECUTION and BATCH_JOB_EXECUTION, next to the STATUS column that holds the BatchStatus. Comparing the two columns is a quick way to spot this bug in an existing system.
What is COMPLETED_WITH_SKIPS?
It is a common custom code, not one built into the framework. A step listener returns it when the step completed but getSkipCount() is above zero, so the flow can send a softer alert, or route to a step that reports the skipped records, while still continuing the job.
8Conclusion
In this answer, we saw that afterStep reports its verdict only through its return value, and that null means 'keep the current status', which was COMPLETED. The fix is to return a custom ExitStatus such as FAILED_VALIDATION, and to define the job as a flow whose transitions route on that exit code to a failure or a quarantine step. We also separated the exit status, which we control and route on, from the BatchStatus, which the framework controls, and saw why returning a status is more reliable than throwing from afterStep.
Open with
The problem is the null. afterStep returns an ExitStatus, and Spring Batch combines it with the step's current status, so null just means keep what you have, which is COMPLETED because the step processed everything without crashing.
Then, if they want more
Logging the error or adding a failure exception at that point does not change the outcome. The listener should return new ExitStatus of FAILED_VALIDATION, or ExitStatus.FAILED, when the invalid ratio is too high, and return the existing status otherwise. Then the job has to act on it.
A plain start-next job only looks at the BatchStatus, so I define a flow: from the load step, on FAILED_VALIDATION go to a quarantine step or fail, on FAILED fail, and on star continue to the next step, so no exit code is left unmatched.
It is worth being clear that exit status and BatchStatus are different things: BatchStatus is the framework's lifecycle state that drives restarts, and the exit status is a code we control for routing and reporting.
I would not throw from afterStep, because depending on the version that exception is logged rather than failing the step; returning a status is the reliable contract.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Batch reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Batch job reports COMPLETED even though a StepExecutionListener's afterStep...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/8c3186d33f/