我试图测试Spring数据JPA存储库(扩展jparepository
)的Spring缓存支持(如本文所述),但我的配置确实有问题。
@Cacheable(value = CacheConfiguration.DATABASE_CACHE_NAME)
Member findByEmail(String email);
@Configuration
@EnableCaching
public class CacheConfiguration implements CachingConfigurer {
public static final String DATABASE_CACHE_NAME = "cache.database";
@Bean
@Override
public CompositeCacheManager cacheManager() {
CompositeCacheManager cacheManager = new CompositeCacheManager(simpleCacheManager());
cacheManager.setFallbackToNoOpCache(Boolean.FALSE);
return cacheManager;
}
@Bean
public CacheManager simpleCacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
List<ConcurrentMapCache> cacheList = new ArrayList<>();
cacheList.add(createConcurrentMapCache(60L, DATABASE_CACHE_NAME, 50L));
simpleCacheManager.setCaches(cacheList);
return simpleCacheManager;
}
private ConcurrentMapCache createConcurrentMapCache(Long timeToLive, String name, long cacheSize) {
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder().expireAfterWrite(timeToLive, TimeUnit.SECONDS);
if (cacheSize >= 0) {
cacheBuilder.maximumSize(cacheSize);
}
ConcurrentMap<Object, Object> map = cacheBuilder.build().asMap();
return new ConcurrentMapCache(name, map, false);
}
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
}
下面是我的测试:
@ActiveProfiles(Profiles.TEST)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { FullIntegrationTestConfiguration.class, BaseTestConfiguration.class, MemberCachingIntegrationTest.Config.class })
public class MemberCachingIntegrationTest {
private static final Member MEMBER_ONE = new Member();
private static final Member MEMBER_TWO = new Member();
@Autowired
private MemberRepository memberRepositoryMock;
@Before
public void setup() {
when(memberRepositoryMock.findByEmail(eq("foo@foo.com"))).thenReturn(MEMBER_ONE);//NPE Here
when(memberRepositoryMock.findByEmail(eq("bar@bar.com"))).thenReturn(MEMBER_TWO);
}
@Test
public void testFindByEmail() {
Member firstInvocation = memberRepositoryMock.findByEmail("foo@foo.com");
assertThat(firstInvocation, is(MEMBER_ONE));
Member secondInvocation = memberRepositoryMock.findByEmail("foo@foo.com");
assertThat(secondInvocation, is(MEMBER_ONE));
verify(memberRepositoryMock, times(1)).findByEmail("foo@foo.com");
}
@Profile(Profiles.TEST)
@Configuration
static class Config {
@Bean
public MemberRepository memberRepositoryMock() {
return mock(MemberRepository.class);
}
}
}
通过运行上面的测试,我注意到SimpleKey
类型的对象作为键传递给ConcurrentMapCache
,而null作为值传递给ConcurrentMapCache
:
@Override
public void put(Object key, Object value) {
this.store.put(key, toStoreValue(value));
}
下面是堆栈跟踪:
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.common.cache.LocalCache.put(LocalCache.java:4210)
at org.springframework.cache.concurrent.ConcurrentMapCache.put(ConcurrentMapCache.java:121)
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:459)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:226)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:181)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:60)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy93.findByEmail(Unknown Source)
at com.bignibouX.tests.repository.member.MemberCachingIntegrationTest.setup(MemberCachingIntegrationTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
您试图在when()中使用的memberRepositoryMock已经是一个带有缓存建议的bean了。
执行以下操作(我不是特别喜欢静态成员,但似乎比使用spring内部的bean更难看):
// this is actuall mock
private static MemberRepository myMock = mock(MemberRepository.class);
// this is mock wrapped behind cache
@Autowired
private MemberRepository memberRepositoryMock;
@Before
public void setup() {
// using real mock for mockito stuff
reset(myMock); // needed for tests where spring config is cached (= mock is not recreated between tests)
when(myMock.findByEmail(eq("foo@foo.com"))).thenReturn(MEMBER_ONE);//NPE Here
when(myMock.findByEmail(eq("bar@bar.com"))).thenReturn(MEMBER_TWO);
}
@Test
public void testFindByEmail() {
// using adviced bean in test
Member firstInvocation = memberRepositoryMock.findByEmail("foo@foo.com");
assertThat(firstInvocation, is(MEMBER_ONE));
Member secondInvocation = memberRepositoryMock.findByEmail("foo@foo.com");
assertThat(secondInvocation, is(MEMBER_ONE));
// again real mock, not bean here
verify(myMock, times(1)).findByEmail("foo@foo.com");
}
@Profile(Profiles.TEST)
@Configuration
static class Config {
@Bean
public MemberRepository memberRepositoryMock() {
return myMock;
}
}
问题内容: 我已经开发了一个Spring Data仓库,扩展了接口有一个方法: 结果由Spring缓存抽象(由)支持缓存。 我的问题是,我想要写一个集成测试(针对HSQLDB)断言结果被从数据库第一次检索,并从缓存中的第二次。 我最初想到模拟jpa基础结构(实体管理器等),并以某种方式断言第二次不调用实体管理器,但似乎太难/麻烦了)。 然后有人可以提供有关如何测试带有注释的Spring Data
我最初想嘲弄一下jpa基础结构(实体管理器等),并以某种方式断言实体管理器不是第二次调用的,但它似乎太难/太累赘了(参见https://stackoverflow.com/A/23442457/536299)。 那么,有人能提供建议,如何测试用注释的Spring数据存储库方法的缓存行为吗?
我想要一个在Spring数据的帮助下创建的存储库(例如)。我不熟悉spring-data(但不熟悉spring),我使用本教程。我选择的处理数据库的技术是JPA2.1和Hibernate。问题是我不知道如何为这样的存储库编写单元测试。 让我们以方法为例。由于我正在进行测试--首先,我应该为它编写一个单元测试--这就是我遇到三个问题的地方: > 首先,如何将的模拟注入到不存在的接口实现中?Sprin
我需要使用spring@Cacheable注释缓存对MongoDB的调用: 不幸的是,使用@Cacheable注释接口中的任何方法都会导致以下异常: 我正在寻找一种方法来缓存对DB的调用(这相当昂贵)。有什么想法吗?
当我启动应用程序时,我得到了错误我的Pom有以下依赖项,我使用的是 下面是我的存储库接口。 当我启动应用程序时,它会抛出错误: 可以有人请建议如果JPA不支持,然后我应该使用什么,任何帮助都是感激的…
我在使用spring存储库和服务的代码中遇到了这个问题,在我的spring项目中,它不适用于用户和角色,这是他们的服务和存储库的问题: 这是用户实现存储库: 这是用户的存储库和服务: 用户服务 试图消除空白,但这是同样的问题,我搜索了很多关于它,但没有找到任何解决方案。