
Spring Boot Testing
检测Test ConfigurationH2
不像Spring Framework使用的@ContextConfiguration来声明主要的Configuration,Spring Boot的 @*Test 注解,如果没有明确定义主要的Configuration的话会自动搜索,搜索逻辑为:
测试类开始往上查找直到找到@SpringBootApplication或者@SpringBootConfiguration
@TestConfiguration 额外的自定义Configuration,不是像@Configuration 会作为主要的Configuration。
When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning.
Test SliceH1
If you use a test annotation to test a more specific slice of your application, you should avoid adding configuration settings that are specific to a particular area on the main method’s application class.
测试Slice默认情况下,不会扫描@Comonent 、@Configuration、@ConfigurationProperties
@ConfigurationH3
Test slices exclude @Configuration classes from scanning.
For example, for a @WebMvcTest, the following configuration will not include the given WebMvcConfigurer bean in the application context loaded by the test slice.
The configuration below will, however, cause the custom WebMvcConfigurer to be loaded by the test slice.
java
@Componentpublic class MyWebMvcConfigurer implements WebMvcConfigurer {// ...}
AutoConfigurationH3
@DataJpaTest 引用了@AutoConfigureDataJpa 注解,该注解会启动的Auto Configuration相关类定义在spring-boot-test-autoconfigure/spring.factories
java
# AutoConfigureDataJpa auto-configuration importsorg.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa=\org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
JPA Auto ConfigurationH2
Spring Boot中使用JPA会自动从@SpringBootApplicaiton 定义的包下搜索实体映射的定义
jsx
// JPABaseConfigurationprotected String[] getPackagesToScan() {List<String> packages = EntityScanPackages.get(this.beanFactory).getPackageNames();if (packages.isEmpty() && AutoConfigurationPackages.has(this.beanFactory)) {packages = AutoConfigurationPackages.get(this.beanFactory);}return StringUtils.toStringArray(packages);}
JpaRepository
同样也是从@EnableAutoConfiguration 注解的包下扫描
java
// AutoConfiguredAnnotationRepositoryConfigurationSource#getBasePackagesprotected Streamable<String> getBasePackages() {return Streamable.of(AutoConfigurationPackages.get(this.beanFactory));}
When a class does not include a package declaration, it is considered to be in the “default package”. The use of the “default package” is generally discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the @ComponentScan, @ConfigurationPropertiesScan, @EntityScan, or @SpringBootApplication annotations, since every class from every jar is read.
ComponentScanH2
In short: Never use @ComponentScan without TypeExcludeFilter - it will start loading everything for all your SpringBootTest tests and also for tests using slices - which beats the purpose of having slices in the first place.
Once you get rid of @ComponentScan (or once you add the mentioned filter):
@SpringBootTestwill load all@Configurationclasses but will not load@TestConfigurationclasses.@TestConfigurationclasses can be picked when needed using@Import.- Test slices like
@WebMvcTestwill not load any extra custom classes / configurations unless you instruct them with@Import. If all your tests with@WebMvcTest(or other slice) need additional configuration then you can easily create your own slice (a separate topic though).
评论
新的评论
上一篇
electron-builder构建镜像
electron-builder构建依赖很多工具,使用一些阉割过的镜像作为runner进行构建会报各种错误 例如,错误一 实际文件是存在的,二进制文件也没有任何问题, ENOENT 错误,不仅仅意味着文件存在或者权限不足,也可能是参数错误,动态库缺失。 通过 ldd 命令可以列…
下一篇
Junit TestInstance
Test Template 用于测试逻辑类似,只是测试数据不同的场景,代码示例 UserIdGeneratorTestInvocationContextProvider 主要实现 TestTemplateInvocationContextProvider 的 provide…
