Spring Component Comment ExcludeFilters ComponentScan not working in Spring Test Test context

I am using Spring Boot 1.4.3.RELEASE and want to exclude some components from being tested when running 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 is loading failed because these classes require AWS to work correctly:

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 work?

+4
source share
1 answer

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

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

    @Test
    public void contextLoads() {
    }
}

Spring Context , AmazonCredentials bean. , , .

, ! , .

+2

Source: https://habr.com/ru/post/1667726/


All Articles