Skip to content

@SpyBean (MockitoPostProcessor) tries to initialize @StepScope Bean out of scope #19020

@elxnis

Description

@elxnis

I think this fix 52050c1 for this issue #17817 has created another issue.

When I try to use @SpyBean on @StepScope bean Spring Boot Test is failing with BeanCreationException. I use mockito-inline to allow spying on proxied class (step scope bean) to stub behaviour. This worked with spring-boot 2.1.7.RELEASE, but stopped working with 2.1.8.RELEASE and onwards.

org.springframework.beans.factory.BeanCreationException: Could not inject field: 
step.scope.spy.bean.batch.MyItemWriter step.scope.spy.bean.JobControllerIT.myItemWriter; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'scopedTarget.writer': 
Scope 'step' is not active for the current thread;
consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; 
nested exception is java.lang.IllegalStateException: 
No context holder available for step scope

Example code to replicate the issue - https://github.com/elxnis/spring-boot-step-scope-spy-bean-issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: regressionA regression from a previous release

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions