微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在类之间缓存Spring的应用程序上下文?

我正在努力提高我正在开发的项目的Spring集成测试的性能.我们正在使用Spring Gradle JUnit.

在build.gradle文件中使用此配置:

test {
    useJUnit()
    setForkEvery(0)
    setMaxParallelForks(1)
}

我们能够在一个JVM中运行所有测试.虽然我认为这是认行为.

但我一直在阅读关于Spring Test Context Caching并在我的application-test.yml中使用此属性

logging:
  level:
    org:
      springframework:
        test:
          context:
            cache: DEBUG

我注意到在同一个类中运行的测试方法的以下日志

2017-09-05 08:33:11.829 DEBUG 5764 --- [    Test worker] c.DefaultCacheAwareContextLoaderDelegate : Storing ApplicationContext in cache under key [THIS HAD SENSITIVE DATA]
2017-09-05 08:33:11.830 DEBUG 5764 --- [    Test worker] org.springframework.test.context.cache   : Spring test ApplicationContext cache statistics: [DefaultContextCache@572e81e7 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 0, missCount = 1]
2017-09-05 08:33:11.849 DEBUG 5764 --- [    Test worker] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [THIS HAD SENSITIVE DATA]
2017-09-05 08:33:11.850 DEBUG 5764 --- [    Test worker] org.springframework.test.context.cache   : Spring test ApplicationContext cache statistics: [DefaultContextCache@572e81e7 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 1, missCount = 1]

还有更多行表示从缓存中检索到的ApplicationContext与密钥….

对于在其他类中运行的测试方法,我注意到类似的日志,例如:

2017-09-05 08:33:12.971 DEBUG 10288 --- [    Test worker] c.DefaultCacheAwareContextLoaderDelegate : Storing ApplicationContext in cache under key [THIS HAD SENSITIVE DATA]
2017-09-05 08:33:12.971 DEBUG 10288 --- [    Test worker] org.springframework.test.context.cache   : Spring test ApplicationContext cache statistics: [DefaultContextCache@2dad6721 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 0, missCount = 1]
2017-09-05 08:33:13.194 DEBUG 10288 --- [    Test worker] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [THIS HAD SENSITIVE DATA]
2017-09-05 08:33:13.194 DEBUG 10288 --- [    Test worker] org.springframework.test.context.cache   : Spring test ApplicationContext cache statistics: [DefaultContextCache@2dad6721 size = 1, maxSize = 32, parentContextCount = 0, hitCount = 1, missCount = 1]

两个类都注释均等:

@RunWith(springrunner.class)
@SpringBoottest
@AutoConfiguremockmvc
@ActiveProfiles({"default", "profile-1", "profile-2"})
public class SomeControllerTest {

    // Test methods...
}

我认为两个类中的测试方法应该可以共享相同的ApplicationContext,从而减少测试持续的时间.但是,有可能这样做吗?如果是这样,怎么样?

我注意到两个ApplicationContext对象大约在08:33:11.829和08:33:12.971的同时存储在缓存中. Test Runner是否在不同的线程或其他东西中执行测试?

解决方法:

您的上下文实际上已缓存,但由于您使用Spring Boot的@MockBean功能,因此您实际上有两个不同的上下文.

@MockBean的使用导致每个ApplicationContext具有不同的唯一键,在该键下,它存储在上下文缓存中.

虽然这可能无法在任何地方公开记录,但实际上在org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory的实现中有内联文档:

We gather the explicit mock deFinitions here since they form part of the MergedContextConfiguration key. Different mocks need to have a different key.

我已经针对Spring Boot打开了一个问题,记录了这种行为:https://github.com/spring-projects/spring-boot/issues/10182

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐