Skip to content

Using @SpyBean with @Qualifer does not work in Spring 1.5.x #16292

@dome313

Description

@dome313

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions