我已经开发了一个Spring Data仓库,MemberRepository
扩展了接口org.springframework.data.jpa.repository.JpaRepository。MemberRepository
有一个方法:
@Cacheable(CacheConfiguration.DATABASE_CACHE_NAME)
Member findByEmail(String email);
结果由Spring缓存抽象(由ConcurrentMapCache
)支持缓存。
我的问题是,我想要写一个集成测试(针对HSQLDB)断言结果被从数据库第一次检索,并从缓存中的第二次。
我最初想到模拟jpa基础结构(实体管理器等),并以某种方式断言第二次不调用实体管理器,但似乎太难/麻烦了)。
然后有人可以提供有关如何测试带有注释的Spring Data Repository方法的缓存行为的建议@Cacheable
吗?
如果你想测试诸如缓存之类的技术方面,请不要使用任何数据库。了解你要在此处测试的内容非常重要。你要确保避免使用完全相同的参数进行方法调用。面向数据库的存储库是与该主题完全正交的方面。
这是我的建议:
Sample
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public 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));
}
}
如你所见,我们在这里做了一些过度测试:
问题内容: 我已经开发了一个Spring Data仓库,扩展了接口有一个方法: 结果由Spring缓存抽象(由)支持缓存。 我的问题是,我想要写一个集成测试(针对HSQLDB)断言结果被从数据库第一次检索,并从缓存中的第二次。 我最初想到模拟jpa基础结构(实体管理器等),并以某种方式断言第二次不调用实体管理器,但似乎太难/麻烦了)。 然后有人可以提供有关如何测试带有注释的Spring Data
我试图测试Spring数据JPA存储库(扩展)的Spring缓存支持(如本文所述),但我的配置确实有问题。 下面是我的测试: 通过运行上面的测试,我注意到类型的对象作为键传递给,而null作为值传递给: 下面是堆栈跟踪:
我想要一个在Spring数据的帮助下创建的存储库(例如)。我不熟悉spring-data(但不熟悉spring),我使用本教程。我选择的处理数据库的技术是JPA2.1和Hibernate。问题是我不知道如何为这样的存储库编写单元测试。 让我们以方法为例。由于我正在进行测试--首先,我应该为它编写一个单元测试--这就是我遇到三个问题的地方: > 首先,如何将的模拟注入到不存在的接口实现中?Sprin
我需要使用spring@Cacheable注释缓存对MongoDB的调用: 不幸的是,使用@Cacheable注释接口中的任何方法都会导致以下异常: 我正在寻找一种方法来缓存对DB的调用(这相当昂贵)。有什么想法吗?
它是否将其存储在缓存中?我有一个应用程序,但应用程序中没有任何地方。属性是提到的db详细信息。我可以通过邮递员存储数据和查询它。