对Spring TestContext缓存进行故障排除

时间:2018-11-12 07:46:45

标签: spring-test

Spring TestContext Framework根据多个键缓存上下文。

如何记录缓存键以确保测试重用上下文?

2 个答案:

答案 0 :(得分:2)

如《 Spring参考手册》的Context Caching部分所述...

  

由于在给定的测试套件中加载大量应用程序上下文会导致该套件花费不必要的长时间执行,因此准确地知道已加载和缓存了多少个上下文通常是有益的。要查看基础上下文缓存的统计信息,可以将org.springframework.test.context.cache日志记录类别的日志级别设置为DEBUG

答案 1 :(得分:1)

除了支持Sam的回答之外,您还可以查看是否在DEBUG上启用了org.springframework.test.context.cache级别:

DEBUG org.springframework.test.context.BootstrapUtils
  Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]

DEBUG org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate
  Storing ApplicationContext in cache under key
  [[MergedContextConfiguration testClass = XTest,
    locations = '{}',
    classes = '{...}',
    contextInitializerClasses = '[...]',
    activeProfiles = '{}',
    propertySourceLocations = '{classpath:application-test.properties}',
    propertySourceProperties = '{...}', contextCustomizers = set[...],
    contextLoader = 'org.springframework.test.context.support.AnnotationConfigContextLoader',
    parent = [null]]]

DEBUG org.springframework.test.context.cache
  Spring test ApplicationContext cache statistics:
  [DefaultContextCache@6ef2f7ad size = 1, maxSize = 32, parentContextCount = 0, hitCount = 0, missCount = 1]

DEBUG org.springframework.test.context.cache
  Spring test ApplicationContext cache statistics:
  [DefaultContextCache@6ef2f7ad size = 1, maxSize = 32, parentContextCount = 0, hitCount = 6, missCount = 1]

很难读取缓存键的详细信息,但是下面的缓存统计信息说明了这一点。