频道
bg

Spring Boot Testing

coding一月 05, 20211mins
Spring Boot Java Test

检测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

@Component
public class MyWebMvcConfigurer implements WebMvcConfigurer {
// ...
}

AutoConfigurationH3

@DataJpaTest 引用了@AutoConfigureDataJpa 注解,该注解会启动的Auto Configuration相关类定义在spring-boot-test-autoconfigure/spring.factories

java

# AutoConfigureDataJpa auto-configuration imports
org.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

// JPABaseConfiguration
protected 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#getBasePackages
protected 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):

  • @SpringBootTest will load all @Configuration classes but will not load @TestConfiguration classes. @TestConfiguration classes can be picked when needed using @Import.
  • Test slices like @WebMvcTest will 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).

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.