Hello all In the Spring boot docs here it says

If there is more than one bean of the requested type, qualifier metadata must be specified at field level:

     @SpyBean
     @Qualifier("example")
     private ExampleService service;

This is not working for me on Spring boot 1.5.19.RELEASE. The simple example is

@SpringBootApplication
public class QualifierspyApplication {
    public static void main(String[] args) {
        SpringApplication.run(QualifierspyApplication.class, args);
    }

    @Primary
    @Bean("simpleService1")
    public SimpleService simpleService1() {
        return new SimpleService("hello 1");
    }

    @Bean("simpleService2")
    public SimpleService simpleService2() {
        return new SimpleService("hello 2");
    }
}

The test is

@RunWith(SpringRunner.class)
@SpringBootTest
public class QualifierspyApplicationFailingTest {
    @SpyBean
    @Qualifier("simpleService2")
    private SimpleService simpleService2;

    @Test
    public void testSpy() {
        Assert.assertEquals("hello 2", simpleService2.method());
    }
}

The test only works if I put @SpyBean(name = "simpleService2"). If i remove @Primary it fails with

java.lang.IllegalStateException: No bean found for definition [SpyDefinition@209da20d name = '', typeToSpy = se.lolotron.qualifierspy.SimpleService, reset = AFTER]

You can find the example here.

Comment From: mbhave

Thanks for the sample. This looks like a bug to me. It's probably this bit which should look at the qualifier when determining the bean name.

Comment From: rhamedy

@mbhave can I work on a fix for this bug?

Comment From: snicoll

@rhamedy thanks for the offer. We're releasing this Friday and I'd like this to be fixed beforehand. If you have time to propose something in the next 2 days, please go ahead.

Comment From: rhamedy

@snicoll I looked into the determineBeanName mentioned in the above comment by @mbhave and I could not figure out how to access the @Qualifier annotation's value in there. The SpyDefinition does not expose anything.

I also came across this issue https://github.com/spring-projects/spring-boot/pull/11077 and while it's not related to this bug it is relating to the @Primary.

I could look into this issue again in the evening (if it is not going to be too late) and it would be nice to get some help/suggestions.

Comment From: rhamedy

I think I have a solution! Basically in the method determineBeanName after the check if (StringUtils.hasText(definition.getName())) I do the following

  • Loop through the passed beans

  • For each bean check if definition.getQualifier() is not null

  • Using the QualifierDefinition.matches(...) to figure out whether the bean name matches the Qualifier value, if true then that's the correct determined bean name

  • If above step failed to return then it proceeds with normal flow which goes and checks for @Primary

Using this solution, the test that is failing in 1.5.x is passing, I need to do some more checks. The if check using QualifierDefinition.matches(...) is a little scary but works. The determineBeanName method would look as follow after the fix

private String determineBeanName(String[] existingBeans, SpyDefinition definition,
            BeanDefinitionRegistry registry) {
        if (StringUtils.hasText(definition.getName())) {
            return definition.getName();
        }

        for (String bean : existingBeans) {
            if (definition.getQualifier() != null) {
                if (definition.getQualifier().matches(
                        (ConfigurableListableBeanFactory) this.beanFactory, bean)) {
                    return bean;
                }
            }
        }

        if (existingBeans.length == 1) {
            return existingBeans[0];
        }
        return determinePrimaryCandidate(registry, existingBeans,
                definition.getTypeToSpy());
    }

The for loop is the new addition? Any feedbacks? @snicoll

Comment From: mbhave

@rhamedy The determineBeanName was just my initial hunch. I hadn't looked at it in detail. I just took a look at the code on master and it's been refactored quite a bit following the issue that you've referenced. This isn't a bug in 2.1.x and master because the qualifier is checked here.

Let's see if the rest of the team thinks this is worth fixing only for 1.5.x.

Comment From: rhamedy

I noticed the 1.5.x tag and I also knew that it is working in master. The fix that I have is for 1.5.x (based off it). Sounds good to me, I will push the PR anyway and will leave it to the team to decide what's best.

Comment From: wilkinsona

Given there's a workaround, I don't think we should fix this in 1.5.x, particularly as it will reach EOL in a little over 4 months' time.

Comment From: philwebb

I agree with Andy. We only really want to touch 1.5.x for critical bugs given the limited life it has left.

Comment From: surapuramakhil

Hie, I am facing the same problem in version 2.1.7

Comment From: snicoll

@surapuramakhil Madhura has indicated above that this should work with 2.1.x. If you think otherwise, please attach a small sample we can run ourselves and we can reopen this issue.

Comment From: surapuramakhil

@snicoll can you try running the below-mentioned test

https://github.com/surapuramakhil/springBootTestDataJpaIssue/blob/master/src/test/java/com/akhil/demo/SpyQualifierTest.java

If you use mock bean instead of spy then bean qualifier works. but ideally, the qualifier should also be working for SpyBean.

when I ran the test I am receiving below output.

9:46:23.876 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.akhil.demo.SpyQualifierTest]
19:46:23.876 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.akhil.demo.SpyQualifierTest]
19:46:23.899 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-18 19:46:24.146  INFO 21715 --- [           main] com.akhil.demo.SpyQualifierTest          : Starting SpyQualifierTest on tech-laptop-bsd with PID 21715 (started by akhil-kfn in /home/akhil-kfn/OpenSource/springBootTestDataJpaIssue)
2020-04-18 19:46:24.147  INFO 21715 --- [           main] com.akhil.demo.SpyQualifierTest          : No active profile set, falling back to default profiles: default
2020-04-18 19:46:24.528  WARN 21715 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder    : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2020-04-18 19:46:24.925  WARN 21715 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dummyController': Unsatisfied dependency expressed through field 'lucky'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'okhttp3.OkHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)}
2020-04-18 19:46:25.223 ERROR 21715 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field lucky in com.akhil.demo.controllers.DummyController required a bean of type 'okhttp3.OkHttpClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
    - @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)

The following candidates were found but could not be injected:
    - User-defined bean


Action:

Consider revisiting the entries above or defining a bean of type 'okhttp3.OkHttpClient' in your configuration.

2020-04-18 19:46:25.227 ERROR 21715 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@294425a7] to prepare test instance [com.akhil.demo.SpyQualifierTest@22fa55b2]

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) [junit-rt.jar:na]
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) [junit-rt.jar:na]
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) [junit-rt.jar:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dummyController': Unsatisfied dependency expressed through field 'lucky'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'okhttp3.OkHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126) ~[spring-boot-test-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 24 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'okhttp3.OkHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1700) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1256) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 42 common frames omitted

ps. lucky is the variable name which I have to okhttp object