gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:10:04 25 4
gpt4 key购买 nike

我正在尝试提高我正在处理的项目的 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]

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

对于在其他类中运行的测试方法,我注意到类似的日志,例如:
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.82908: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

关于spring - 是否可以在类之间缓存 Spring 的应用程序上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46060579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com