Skip to content

Support field name as fallback qualifier for @TestBean, MockitoBean, and MockitoSpyBean #32939

Closed
@sbrannen

Description

@sbrannen

@Qualifier support was recently added for @TestBean, MockitoBean, and MockitoSpyBean fields; however, when the name of the annotated field exactly matches the name of the bean being overridden or spied the field name is not currently used as a fallback qualifier, in contrast to regular injection points (such as @Autowired fields).

For example, the following currently only passes with the presence of @Qualifier("exampleService").

@SpringJUnitConfig
class MockitoBeanFallbackQualifierTests {

	@Qualifier("exampleService")
	@MockitoBean
	ExampleService exampleService;

	@Test
	void test() {
		when(exampleService.greeting()).thenReturn("test");
		assertThat(exampleService.greeting()).isEqualTo("test");
	}

	@Configuration
	static class Config {

		@Bean
		ExampleService exampleService() {
			return () -> "prod";
		}

		@Bean
		ExampleService exampleService2() {
			return () -> "prod2";
		}
	}

	interface ExampleService {
		String greeting();
	}
}

Whereas, ideally we should be able to remove @Qualifier("exampleService") and have the field name ("exampleService") be used as the fallback qualifier.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions