Skip to content
Advertisement

Create @MockBean with qualifier by annotating class?

In my Spring Boot test I’m using 2 mock beans with different qualifiers:

@RunWith(SpringRunner.class)
@SpringBootTest
class HohoTest {
    @MockBean @Qualifier("haha") IHaha ahaha;
    @MockBean @Qualifier("hoho") IHaha ohoho;
}

Since I’m not using these beans explicitly, I would rather move them away from the class body, as the @MockBean annotation is now repeatable:

@RunWith(SpringRunner.class)
@SpringBootTest
@MockBean(IHaha.class)
@MockBean(IHaha.class)
class HohoTest {}

However, I need to pass in a qualifier as well, since they have the same type. Any idea on how I can achieve that?

Advertisement

Answer

When declaring @MockBean at the class level, there is currently no support for providing a qualifier.

If you would like to have such support, I suggest you request it in the Spring Boot issue tracker.

Otherwise, you will need to continue declaring @MockBean on fields alongside @Qualifier.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement