Spring Boot Unit Test
2017-11-14 12:15
411 查看
Nested Configuration Class
If we just want to test a simple component, we can use @RunWith(SpringRunner.class) along with @TestConfiguration on an inner static class.@RunWith(SpringRunner.class) public class SimpleComponentTest { @TestConfiguration @ComponentScan(basePackages = { "com.sap.mcp.collaboration.mapper", "com.sap.mcp.common.mapper" }) static class MapperTestContextConfiguration { public ObjectMapper objectMapper() { return new ObjectMapper(); } } }
The key point in the above code snippet is the @TestConfiguration, quote from spring reference doc:
If you want to customize the primary configuration, you can use a nested @TestConfiguration class. Unlike a nested @Configuration class which would be used instead of a your application’s primary configuration, a nested @TestConfiguration class will be used in addition to your application’s primary configuration.
In other word, if you’re using @Configuration on a nested static class then this nested class is your primary configuration class which doesn’t require an additional @SpringBootConfiguration class. But if you’re using @TestConfiguration on a nested static class then this configuration is just a complement to your primary configuration but with higher priority, and it requires you have a class annotated with @SpringBootConfiguration.
If you’re using any Spring Boot ***Test @interface such @SpringBootTest, @DataJpaTest and so on without nested configuration class, Spring boot will auto search @Configuration class for you.
Remember that even inner @Configuration or @TestConfiguration will be scaned as configuration if they’re included in scan packages. And it’s very likely that test cases’ configuration will affect each other if the test package has the same name as production package.
Test Component
@TestConfiguration and @TestComponentTest component can be used to define additional beans or
customizations for a test. Unlike regular @Configuration classes the use of
@code @TestConfiguration does not prevent auto-detection of @SpringBootConfiguration
Test component will be excluded during component scan if you are using @SpringBootApplication, because @ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class) and Spring has provided a default implementation named TestTypeExcludeFilter. So remember to add TypeExcludeFilter if you’re not using @SpringBootApplication
Fine-tunning Test Configuration
Auto-configuration customizationClasspath scanning tunning
Test bootstrap
Detail see https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4
相关文章推荐
- Spring Boot UnitTest
- Spring Boot的单元测试(Unit Test)
- maven打包springboot项目跳过test文件夹
- Spring下的Unit Test Notes 2nd
- springboot Test 自动配置注解详单
- SpringBoot+Junit Test
- spring unit test start error
- SpringBoot Test集成测试
- Spring下的Unit Test笔记
- spring-boot-maven-plugin 插件,关闭自动test
- Spring Cloud# Spring Cloud Stream UnitTest设计模式
- JSONAssert & Spring Boot Test
- SpringbootControllerTest
- Spring下的Unit Test Notes 2nd
- org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type
- Spring boot test
- springboot test
- org.springframework.boot.test.SpringApplicationConfiguration;
- springboot测试环境中为什么会将mongodb的数据自动存储到test表中
- 009 spring mvc controller unit test