代码之家  ›  专栏  ›  技术社区  ›  Rubén Jiménez Goñi

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

  •  4
  • Rubén Jiménez Goñi  · 技术社区  · 8 年前

    此配置位于 build.gradle

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

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

    Spring Test Context Caching 在我的应用测试中使用这个属性。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

    我都注意到了 应用程序上下文 对象大约同时存储在缓存中 08:33:11.829 08:33:12.971 . 测试运行程序是否在不同的线程中执行测试?

    1 回复  |  直到 8 年前
        1
  •  6
  •   Sam Brannen    8 年前

    实际上,您的上下文是缓存的,但由于您使用Spring Boot的,所以实际上有两个不同的上下文 @MockBean 特色

    每个原因 ApplicationContext 有一个不同的

    虽然这可能不会在任何地方公开记录,但事实上,在实施过程中有在线记录 org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory :

    我们在这里收集显式模拟定义,因为它们构成MergedContextConfiguration键的一部分。不同的mock需要不同的键。

    我已针对Spring Boot发布了一个问题,以记录此行为: https://github.com/spring-projects/spring-boot/issues/10182