In our application, we have several JmsTemplate beans. The @MockBean is intended to replace or create a bean in the application context with the matching type. In current implementation, we cannot just do this:
@Qualifier
@MockBean
private JmsTemplate verySpecificJmsTemplate;
It would even produce an error:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available: expected single matching bean but found 3: firstJmsTemplate,secondJmsTemplate,thirdJmsTemplate
What we really want is to replace a very specific bean. Assuming JmsTemplate is annotated with the corresponding @Qualifer annotation, it would be great if @MockBean would work along with @Qualifier, in this case, the framework would understand that we actually just want to substitute the very specific real bean with the mock - the one annotated with the same qualifier.
P.S: Although we are using spring-boot-test-2.3.12, this problem seems to persist in the newer versions as well.
Comment From: wilkinsona
You can use the name attribute on @MockBean to select a specific bean to mock:
@MockBean(name = "secondJmsTemplate")
private JmsTemplate verySpecificJmsTemplate;
Beyond this, Spring Boot's @MockBean support is going to be deprecated in 3.4 in favour of the new @MockitoBean support in Framework 6.2 so no further enhancements are planned.