Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring ComponentScan excludeFilters annotation not working in Spring Boot Test context

I am using Spring Boot 1.4.3.RELEASE and want to exclude some components from being scanned when running the tests.

@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan(
        basePackages = {"com.foobar"},
        excludeFilters =  @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {AmazonKinesisRecordChecker.class, MyAmazonCredentials.class}))
public class ApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Despite the filters, when I run the test the unwanted components are loaded and Spring Boot crashes as those classes require an AWS environment to work properly:

2017-01-25 16:02:49.234 ERROR 10514 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amazonKinesisRecordChecker' defined in file 

Question: how can I make the filters working?

like image 791
Javide Avatar asked Jan 25 '17 05:01

Javide


People also ask

What is the use of component scan in Spring Boot?

One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

What is the annotation filter type in spring component scans?

The ANNOTATION filter type includes or excludes classes in the component scans which are marked with given annotations. Let's say, for example, that we have an @Anima l annotation: Finally, let's use the FilterType.ANNOTATION to tell Spring to scan for @Animal -annotated classes: As we can see, the scanner picks up our Elephant just fine:

How to use spring include filter to auto detect a component?

Using spring include filter, it is possible to auto detect by @ComponentScan. Find the UserService . Now to filter component scanning, we here using annotation filter type in JavaConfig.

When to use @springboottest annotation?

If we are writing unit tests then it’s always better now to use @SpringBootTest annotation. Rather use the specialized spring boot test annotations which test a very specific slice of the application. These annotations disable full auto-configuration and instead apply only configuration relevant to specific layers of the codebase.


1 Answers

What you need is, not to exclude them but to mock them instead, using @MockBean. As shown below

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
    @MockBean
    AmazonCredentials amazonCredentials;

    @Test
    public void contextLoads() {
    }
}

This way you will let Spring Context know that you want to mock the AmazonCredentials bean. Sometimes, excluding filters is a bit tricky to work with.

Hope this helps! I would love to explore if we have another way to get this done.

like image 174
dagra Avatar answered Oct 19 '22 18:10

dagra