栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Spring数据存储库上测试Spring的声明式缓存支持?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Spring数据存储库上测试Spring的声明式缓存支持?

如果你想测试诸如缓存之类的技术方面,请不要使用任何数据库。了解你要在此处测试的内容非常重要。你要确保避免使用完全相同的参数进行方法调用。面向数据库的存储库是与该主题完全正交的方面。

这是我的建议:

  1. 设置用于配置声明性缓存的集成测试(或从生产配置中导入必要的位和段)。
  2. 配置存储库的模拟实例。
  3. 编写测试用例以设置模拟的预期行为,调用方法并相应地验证输出。

Sample

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfigurationpublic class CachingIntegrationTest {  // Your repository interface  interface MyRepo extends Repository<Object, Long> {    @Cacheable("sample")    Object findByEmail(String email);  }  @Configuration  @EnableCaching  static class Config {    // Simulating your caching configuration    @Bean    CacheManager cacheManager() {      return new ConcurrentMapCacheManager("sample");    }    // A repository mock instead of the real proxy    @Bean    MyRepo myRepo() {      return Mockito.mock(MyRepo.class);    }  }  @Autowired CacheManager manager;  @Autowired MyRepo repo;  @Test  public void methodInvocationShouldBeCached() {    Object first = new Object();    Object second = new Object();    // Set up the mock to return *different* objects for the first and second call    Mockito.when(repo.findByEmail(Mockito.any(String.class))).thenReturn(first, second);    // First invocation returns object returned by the method    Object result = repo.findByEmail("foo");    assertThat(result, is(first));    // Second invocation should return cached value, *not* second (as set up above)    result = repo.findByEmail("foo");    assertThat(result, is(first));    // Verify repository method was invoked once    Mockito.verify(repo, Mockito.times(1)).findByEmail("foo");    assertThat(manager.getCache("sample").get("foo"), is(notNullValue()));    // Third invocation with different key is triggers the second invocation of the repo method    result = repo.findByEmail("bar");    assertThat(result, is(second));  }}

如你所见,我们在这里做了一些过度测试:

  1. 我认为最相关的检查是第二个调用返回第一个对象。这就是缓存的全部内容。使用相同键的前两个调用返回相同的对象,而使用不同键的第三个调用导致在存储库上进行第二次实际调用。
  2. 我们通过检查缓存实际上是否具有第一个键的值来增强测试用例。甚至可以将其扩展为检查实际值。另一方面,我也认为避免这样做是很好的,因为你倾向于测试更多的内部机制而不是应用程序级别的行为。
    重要要点
  3. 你不需要任何基础结构即可测试容器行为。
  4. 设置测试用例很简单。
  5. 精心设计的组件使你可以编写简单的测试用例,并需要较少的集成工作来进行测试。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号