Skip to content

@MockBean does not work for @RefreshScope bean #5724

Closed

Description

@colin-shine

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

  1. added
    status: waiting-for-triage
    on Apr 18, 2016
  2. colin-shine commented on Apr 18, 2016

    @colin-shine
    Author

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

  3. colin-shine commented on Apr 20, 2016

    @colin-shine
    Author

    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?

  4. spencergibb commented on Apr 20, 2016

    @spencergibb
    Member

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

  5. removed
    status: waiting-for-triage
    on Apr 21, 2016
  6. added this to the 1.4.0.M3 milestone on Apr 21, 2016
  7. philwebb commented on Apr 21, 2016

    @philwebb
    Member

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

  8. colin-shine commented on Apr 25, 2016

    @colin-shine
    Author

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

  9. philwebb commented on Apr 25, 2016

    @philwebb
    Member
  10. self-assigned this
    on May 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions