Using Mockito will cause dependency injection to fail when multiple beans have circular dependencies.View source found that org. Springframework. Boot. Test. The mock. Mockito. MockitoPostProcessor. SpyPostProcessor the class in the IoC container callback getEarlyBeanReference method of its implementation, will create a proxy for early exposure of the Bean.And in its implementation postProcessAfterInitialization method to create an agent, this will cause the failure of injection, why do you want to do that? As the Spring AOP AbstractAutoProxyCreator Bean exposure will create in the early processing agent, but in postProcessAfterInitialization approach is no longer for the early exposure of beans to create a proxy object.Looking forward to your reply!Thank you very much!

Comment From: snicoll

when multiple beans have circular dependencies

That doesn't sound a sane setup to me. I know that the ApplicationContext can manage some of those cycles transparently for you but I am afraid it'll be hard to provide support without a small sample that reproduces the problem. Can you please share one, either as a zip attached to this issue or by providing the link to a GitHub repository. Thank you.

Comment From: xiaobeijiang

springboot-mockito-demo.zip Thank you very much for your reply! This is the fault I restored.

Comment From: snicoll

@xiaobeijiang thanks for the sample. This setup is quite unusual and I am amazed that the framework manages to deal with it to be honest. Please consider breaking this cycle between A and B regardless of the outcome of this issue. The issue only happens when you want to spy on A and B in the same test so we'll have to investigate if we support that scenario.

Comment From: wilkinsona

I don't think we should do anything to try and support spying on beans with circular dependencies. The code's already quite complex.

Comment From: philwebb

We discussed this today as a team and I'm afraid this isn't something we feel we can support.

Comment From: xiaobeijiang

Ok, thank you for your reply