You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a @MockitoBean is annotated with a custom @Qualifier it cannot be injected into a @Configuration class when there is a normal bean with said qualifier.
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.example.qualifiermockitobeanbug.QualifierAnnotationTest$IExample' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.example.qualifiermockitobeanbug.QualifierAnnotationTest.MyQualifier()}
Workaround
Replacing @MockitoBean with @MockBean causes the test to succeed.
The text was updated successfully, but these errors were encountered:
sbrannen
changed the title
MockitoBean with Qualifier is not injected into Configuration@MockitoBean with custom @Qualifier is not injected into @Configuration class
Mar 25, 2025
Congratulations on submitting your first issue for the Spring Framework! 👍
And thanks for the well-crafted reproducer.
This turns out to be an interesting case. We have support for @Qualifier and various tests which verify that support; however, we did not have a test which exercises the scenario you have presented.
The core of the issue is that your toBeReplacedByMock() method declares QualifierExample as the return type (which is in fact annotated with @MyQualifier. However, you explicitly instructed Spring to create a mock based on the IExample interface via the @MockitoBeanmyExample field in the test class. Thus, the IExample mock effectively replaces what would otherwise have been a QualifierExample in the ApplicationContext.
In other words, in your example there is no longer a bean of type IExample in the context which is also annotated with @MyQualifier. Hence, the exception you encountered.
To work around this with @MockitoBean, you have a few options.
Either annotate the toBeReplacedByMock() method with @MyQualifier.
Or change the type of the myExample field to QualifierExample and remove the @MyQualifier declaration on the field.
Or change the return type of the toBeReplacedByMock() method to IExample and annotate the method with @MyQualifier.
The latter two options are preferable, since they do not result in different types of beans being present in the final state of the ApplicationContext; whereas, option 1 (as well as your unmodified example when using Spring Boot's @MockBean) results in no bean of type QualifierExample in the ApplicationContext (as mentioned above), which could lead to bugs depending on what other components expect to be available in the context.
Although there are workarounds, we do consider this a bug since it worked in Spring Boot. Furthermore, we aim to have consistent support for custom @Qualifier annotations with the Bean Override support in Spring Framework as well. Consequently, I have been working on a fix for this that I will soon commit.
Problem
When a
@MockitoBean
is annotated with a custom@Qualifier
it cannot be injected into a@Configuration
class when there is a normal bean with said qualifier.Reproducer
Reproducer Test
The test fails with:
Workaround
Replacing
@MockitoBean
with@MockBean
causes the test to succeed.The text was updated successfully, but these errors were encountered: