Skip to content

Unable to test step inside flow #4771

@fmbenhassine

Description

@fmbenhassine
Contributor

Discussed in #4518

Originally posted by heitormsilva December 16, 2023
Hi.

I have a configuration like this: job -> step (outerStep) -> flow (outerFlow) -> flow (innerFlow) -> step (innerStep).

I want to run a integration test of the innerStep, but I get a "java.lang.IllegalStateException: No Step found with name: [innerStep]".

If I run the test pointing to outerStep it works.

My test class is annotated with @SpringBootTest and @SpringBatchTest.
I'm running the step with: jobLauncherTestUtils.launchStep("innerStep");

How can I call only the innerStep?

Activity

HyunSangHan

HyunSangHan commented on May 8, 2025

@HyunSangHan

I can take a look at this! 👀

fmbenhassine

fmbenhassine commented on May 8, 2025

@fmbenhassine
ContributorAuthor

@HyunSangHan Sure! Thank you for your offer to help!

As I mentioned in the initial discussion, this seems like a bug to me. The test utility should be able to locate the step no matter how deep it is in the flow. Please let me know if you need help on this.

HyunSangHan

HyunSangHan commented on May 12, 2025

@HyunSangHan

Let me first share what I’ve understood so far about the issue like below:
(If I got anything wrong, feel free to let me know.)


When calling getStep(stepName), it looks for a step with the given name among the available steps.
At this point, only the outerStep is typically included in the list of steps(innerStep is usually not).

However, there's only one condition where we get a chance to retrieve steps inside another step (like innerStep within outerStep).

else if (step instanceof StepLocator) {
Step result = ((StepLocator) step).getStep(stepName);
if (result != null) {
return result;
}
}

But in the code, this only happens if the outerStep implements StepLocator.
If it doesn't, we miss that opportunity.

From what I can tell, classes like FlowStep (used as outerStep) do not implement StepLocator, so their inner steps are not exposed—leading to this issue.

HyunSangHan

HyunSangHan commented on May 24, 2025

@HyunSangHan

@fmbenhassine
I’ve taken a closer look at this issue and I have two ideas to enable reading inner steps.

The first idea is to have the abstract class AbstractStep implement StepLocator. I believe this is a more fundamental solution because inner steps can only be included in the steps managed by the outer step if the outer step is an instance of StepLocator. However, the downside is that all steps extending AbstractStep would need to implement the methods of StepLocator.

The second idea is to inject applicationContext as a dependency into JobLauncherTestUtils and use the getBean method to retrieve the step by looking up the bean corresponding to the inner step’s name. This would involve relatively less code modification, but I feel this may not be a good pattern since it relies on fetching the bean from applicationContext.

Considering these two approaches, I believe the first idea would be a better solution to this issue.
I’d like to hear your thoughts on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @fmbenhassine@HyunSangHan

        Issue actions

          Unable to test step inside flow · Issue #4771 · spring-projects/spring-batch