Repository navigation
Closed
Milestone
Description
Sample project:
https://github.com/colin-shine/mockbean-issue
Run the MyTest to see the problem.
Spring Boot: v1.4.0.M2
Spring Cloud Config: 1.1.0.RC2
@MockBean detects 2 beans instead of one:
java.lang.IllegalStateException: Unable to register mock bean issues.spring.shine.colin.BeanB expected a single existing bean to replace but found [beanB, scopedTarget.beanB]
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.getBeanName(MockitoPostProcessor.java:218)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.registerMock(MockitoPostProcessor.java:171)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.register(MockitoPostProcessor.java:161)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:131)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:119)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:680)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:104)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 27 more
Activity
colin-shine commentedon Apr 18, 2016
FYI, I currently use this approach, which works perfectly. The
@Primarymock takes precedence over the 2 beans (the proxy and target).colin-shine commentedon Apr 20, 2016
I've made a stab at a fix here, using a
ContextCustomizerand aBeanFactoryPostProcessor.It gets all
@MockBeanfields from the test class and looks for any@RefreshScopebeans of the same type. If any are found, the bean definitions are removed, which removes the duplicate causing the problem.Am I on the right track here? Would this form the basis of some kind of test auto configuration in the Spring Cloud Config project?
spencergibb commentedon Apr 20, 2016
BTW,
@RefreshScopeis part of spring-cloud-commons, not spring-cloud-config.philwebb commentedon Apr 21, 2016
The
scopedTarget.prefix is used by ScopedProxyUtils so I think it's safe to just filter those from the candidates.colin-shine commentedon Apr 25, 2016
@philwebb I have created an Issues Pull Request incorporating your feedback.
philwebb commentedon Apr 25, 2016
Thanks @colin-shine