Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@MockBean does not work for @RefreshScope bean #5724

Closed
colin-shine opened this issue Apr 18, 2016 · 6 comments
Closed

@MockBean does not work for @RefreshScope bean #5724

colin-shine opened this issue Apr 18, 2016 · 6 comments
Assignees
Milestone

Comments

@colin-shine
Copy link

colin-shine commented Apr 18, 2016

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
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 18, 2016
@colin-shine
Copy link
Author

FYI, I currently use this approach, which works perfectly. The @Primary mock takes precedence over the 2 beans (the proxy and target).

@colin-shine
Copy link
Author

colin-shine commented Apr 20, 2016

I've made a stab at a fix here, using a ContextCustomizer and a BeanFactoryPostProcessor.

It gets all @MockBean fields from the test class and looks for any @RefreshScope beans 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
Copy link
Member

BTW, @RefreshScope is part of spring-cloud-commons, not spring-cloud-config.

@philwebb philwebb removed the status: waiting-for-triage An issue we've not yet triaged label Apr 21, 2016
@philwebb philwebb added this to the 1.4.0.M3 milestone Apr 21, 2016
@philwebb
Copy link
Member

The scopedTarget. prefix is used by ScopedProxyUtils so I think it's safe to just filter those from the candidates.

@colin-shine
Copy link
Author

@philwebb I have created an Issues Pull Request incorporating your feedback.

@philwebb
Copy link
Member

Thanks @colin-shine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants