简体   繁体   中英

Spring Boot: load all beans implementing an interface in test?

I have some interface Filter and some implementations of this interface.

In @Test , I'd like to load all beans implementing Filter .

Currently, I have to declare them explicitly. For example:

@ContextConfiguration(classes = {FilterA.class, FilterB.class})

The problem: Introducing another Filter would require adding it for each test class that uses the filters .

Is there a better way?

Add a TestConfiguration and declare all the beans implementing the interface in the configuration. Then add an @Import(TestConfiguration.class) in your tests.

That way if there is an extra implementation you only add it in the TestConfiguration .

Only thing you need to take care of is adding the appropriate implementation of the interface with use of the @Qualifier annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM