Reputation: 2657
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?
Upvotes: 9
Views: 10709
Reputation: 650
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.
Upvotes: 5