use*_*608 5 java grails unit-testing mocking redis
在我的控制器中,我有如下代码.RedisTemplate stringRedisTemplate
def accessRedis()
{
val = stringRedisTemplate.opsForValue().get('Key')
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器测试中,我打算注入一个模拟的RedisTemplate,它返回一个模拟的ValueOperations.我的代码:
def template = mockFor(RedisTemplate)
def val = mockFor(org.springframework.data.redis.core.ValueOperations)
val.demand.get {p-> println "$p"}
template.demand.opsForValue {
return val.createMock()
}
controller.stringRedisTemplate = template.createMock()
controller.accessRedis()
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法使用类com.tnd.viewport.ui转换对象'com.tnd.viewport.ui.AppHawkControllerSpec$_$spock_feature_0_1_closure2@1aa55dd5' .AppHawkControllerSpec $ _ $ spock_feature_0_1_closure2'到类'org.springframework.data.redis.core.ValueOperations'
你能为我的方案建议一个解决方案吗?谢谢!
小智 1
redisTemplate = mock(RedisTemplate.class);
Whitebox.setInternalState(loginService, "redisTemplate", redisTemplate);
List<Object> list = new ArrayList<Object>();
list.add(15l);
List<Object> keys = new ArrayList<>();
keys.addAll(Arrays.asList("15"));
HashOperations<Serializable, Object, Object> hashOperations =mock(HashOperations.class);
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(hashOperations.multiGet(anyString(), anyListOf(Object.class))).thenReturn(list);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3438 次 |
| 最近记录: |