是否可以在类之间缓存Spring的Application Context? [英] Is it possible to cache Spring's Application Context between classes?

查看:114
本文介绍了是否可以在类之间缓存Spring的Application Context?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高我正在研究的项目的Spring集成测试的性能.我们正在使用Spring + Gradle + JUnit.

I'm trying to improve the performance of the Spring integration tests of a project I'm working on. We're using Spring + Gradle + JUnit.

build.gradle文件中具有以下配置:

With this configuration inside the build.gradle file:

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

我们能够在单个JVM中运行所有测试.尽管我认为这是默认行为.

We're able to run all tests in a single JVM. Though I think this is the default behaviour.

但是我一直在阅读有关

But I've been reading about Spring Test Context Caching and with this property in my application-test.yml:

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

我注意到以下日志记录了同一类中运行的测试方法

I noticed the following logs for test methods running in the same class

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]

还有更多的行显示Retrieved ApplicationContext from cache with key....

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

For tests methods running in other classes, I noticed similar logs, for example:

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,从而减少了测试持续的时间.但是,有可能这样做吗?如果可以,怎么办?

And I think it should be possible for test methods in both classes to share the same ApplicationContext thus decreasing the amount of time the tests last for. But, is it possible to do that? If so, how?

我注意到,两个ApplicationContext对象大约在同一时间08:33:11.82908:33:12.971都存储在缓存中.测试运行器是否在不同的线程中执行测试?

I noticed both ApplicationContext objects are stored in the cache at around the same time 08:33:11.829 and 08:33:12.971. Does the Test Runner execute the tests in different threads or something?

推荐答案

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

Your contexts are in fact cached, but you effectively have two different contexts since you use Spring Boot's @MockBean feature.

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

The use of @MockBean causes each ApplicationContext to have a different unique key under which it is stored in the context cache.

尽管可能未在任何地方公开记录此文档,但实际上org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory的实现中存在内联文档:

Although this may not be publicly documented anywhere, there is in fact in-line documentation in the implementation of org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory:

我们在这里收集了明确的模拟定义,因为它们构成了MergedContextConfiguration键的一部分.不同的模拟需要具有不同的密钥.

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

I have opened an issue against Spring Boot to have this behavior documented: https://github.com/spring-projects/spring-boot/issues/10182

这篇关于是否可以在类之间缓存Spring的Application Context?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆