当前位置: 首页 > 知识库问答 >
问题:

测试中的自发启动Spring Boot上下文

羊煜
2023-03-14

我在spring TestContext配置的深潜过程中遇到了spring-boot-test问题。

示例项目:github示例项目

我有三个带有bean的Spring配置(config1-重量级配置,我需要缓存它)

@ContextHierarchy({
    @ContextConfiguration(classes = {Config1.class}),
    @ContextConfiguration(classes = {Config2.class}),
})
Started ConfigTest1

两次然后

Started ConfigTest2

只有一次。在我看来,这是可能的,因为configtest1已经缓存为TestContext(发生这种情况是因为configtest1configtest2包含在同一个包中)

这意味着spring启动我的spring上下文三次!为什么?在日志中查看三次spring boot logo:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)

更新1:

多谢.但是,如果我在@SpringBootTest中在两个测试类中以不同的顺序设置属性(或类),则不会缓存任何配置:(

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextHierarchy({
    @ContextConfiguration(classes = {Config1.class}),
    @ContextConfiguration(classes = {Config2.class}),
})
public class ConfigTest1 {

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "property1=1")
@ContextHierarchy({
    @ContextConfiguration(classes = {Config1.class}),
    @ContextConfiguration(classes = {Config3.class}),
})
public class ConfigTest2 {

共有1个答案

邹玄裳
2023-03-14

一切都在正常运转!

您之所以感到困惑,是因为Spring Boot记录了为其加载ApplicationContext的测试类的名称。因此,您会在日志中两次看到configtest1,因为为该测试类加载了两个上下文。

如果将logging.level.org.springframework.test.context.cache=debug添加到application.properties中,您将看到以下日志输出。

DEBUG ... c.DefaultCacheAwareContextLoaderDelegate : Storing ApplicationContext in cache under key [[WebMergedContextConfiguration@7a765367 testClass = ConfigTest1, locations = '{}', classes = '{class spring.test.mistake.delete.Config1}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@643b1d11, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6d00a15d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@475530b9], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]]
DEBUG ... c.DefaultCacheAwareContextLoaderDelegate : Storing ApplicationContext in cache under key [[WebMergedContextConfiguration@52feb982 testClass = ConfigTest1, locations = '{}', classes = '{class spring.test.mistake.delete.Config2}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@643b1d11, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6d00a15d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@475530b9], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [WebMergedContextConfiguration@7a765367 testClass = ConfigTest1, locations = '{}', classes = '{class spring.test.mistake.delete.Config1}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@643b1d11, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6d00a15d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@475530b9], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]]]
DEBUG ... c.DefaultCacheAwareContextLoaderDelegate : Storing ApplicationContext in cache under key [[WebMergedContextConfiguration@5ddcc487 testClass = ConfigTest2, locations = '{}', classes = '{class spring.test.mistake.delete.Config3}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@643b1d11, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6d00a15d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@475530b9], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [WebMergedContextConfiguration@44c73c26 testClass = ConfigTest2, locations = '{}', classes = '{class spring.test.mistake.delete.Config1}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.SpringBootTestContextCustomizer@643b1d11, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6d00a15d, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@475530b9], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]]]
DEBUG ... org.springframework.test.context.cache   : Spring test ApplicationContext cache statistics: [DefaultContextCache@478db956 size = 3, maxSize = 32, parentContextCount = 1, hitCount = 11, missCount = 3]
 类似资料:
  • 使用spring-boot时,一切工作都很好。尽管如此,在spring-boot中已删除了注释和。我试图将代码重构为新版本,但我做不到。对于以下测试,我的应用程序在测试之前没有启动,http://localhost:8080返回404: 如何重构测试以使其在Spring-Boot1.5中工作?

  • 我想不通..出于某种原因,Spring Junit没有将我的bean添加到上下文中。请砰!! 由:org.springframework.beans.factory.nosuchBeanDefinitionException引起:没有类型为“com.api.demo.store.FileStorage”的合格bean可用:需要至少有1个bean作为autowire候选bean。依赖项注释:{@or

  • 我在spring boot应用程序中实现了firebase消息传递。为此,我创建了一个类似本文所述的bean,以便轻松访问实例。然后,该bean被注入到服务中。然后,当调用利用bean的服务方法时,所述服务被注入到我的控制器中。 现在我的问题是该项目的测试套件(使用JUnit5 根本原因: 注意:这些错误只有在整体运行testsuite时才会出现。如果我自己运行一个测试类,则不会出现错误。 解决这

  • 我正在尝试编写一个单元测试到一个自定义反序列化器,该反序列化器是使用一个带有@了的构造函数来实例化的,并且我的实体标有@JsonDesri的。它在我的集成测试中工作得很好,其中MockMvc会带来Spring serverside。 然而,在调用objectMapper.read值(…)的测试中,使用不带参数的默认构造函数的反序列化器的新实例被实例化。即使 实例化有线版本的反序列化程序,实际调用仍

  • 运行默认Spring Boot单元测试时: 我收到此错误: 我正在运行Spring Boot 2.6.2,使用Flyway和H2,以及这些依赖项: 这是演示项目:https://github.com/filip194/demo-h2-flyway/. 我不确定,也不是专家,但也许与Hibernate有一些关联,我无法理解。我试图为H2创建application.properties的测试资源文件夹

  • 我有以下课程: 应用和配置类 MyRestController类 我的实用类 当我启动应用程序并将其作为独立jar运行时,或者从IDE(Eclipse)运行时,没有任何问题,一切都按预期进行。 然而,我想写一个单元测试来测试我的MyRestController类。。。我得到了一个NPE,因为自动连接字段util为null(在MyRestController类中)。 这是我的测试课: 我肯定错过了一