spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
73.99k stars 40.4k forks source link

A @SpyBean on the output of a FactoryBean is not reset #31204

Open nightswimmings opened 2 years ago

nightswimmings commented 2 years ago

Repository SpyBeans seem to keep stubs when context is reused on other test class, since 2.6.x up to 2.7.0, at least

This does not seem to happen with regular beans (i.e, not JPA proxies Repository interfaces), and it seems caused by MockReset.get(bean) not returning AFTER in objectResetMocksTestExecutionListener.resetMocks, when it should. It returns NONE because SpringBootMockResolver does not find the target, apparently test.zip .

wilkinsona commented 2 years ago

Thanks for the sample, @nightswimmings. As far as I can tell, the problem is broader than proxied Spring Data Repository interfaces. It appears to apply whenever @SpyBean is used on the output of a FactoryBean.

@MockBean is not affected as when the mock is created, it's added to MockitoBeans from where it can be retrieved and reset. Also adding spies to MockitoBeans may be a solution but I'll need to give it and any potential unwanted side-effects some more thought.