In my controller I have code like the following. RedisTemplate stringRedisTemplate
def accessRedis()
{
val = stringRedisTemplate.opsForValue().get('Key')
}
In my controller Test, I intent to inject a mocked RedisTemplate that return a mocked ValueOperations. My code:
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()
However, I got the following error: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.tnd.viewport.ui.AppHawkControllerSpec$_$spock_feature_0_1_closure2@1aa55dd5' with class 'com.tnd.viewport.ui.AppHawkControllerSpec$_$spock_feature_0_1_closure2' to class 'org.springframework.data.redis.core.ValueOperations'
Could you advice a solution for my scenario? Thanks!
It's simple. Mock them separately.
Eg.
@MockBean
private RedisTemplate<String, String> redisTemplate;
@MockBean
private ValueOperations valueOperations;
@Test
public void testRedisGetKey() {
// This will make sure the actual method opsForValue is not called and mocked valueOperations is returned
doReturn(valueOperations).when(redisTemplate).opsForValue();
// This will make sure the actual method get is not called and mocked value is returned
doReturn("someRedisValue").when(valueOperations).get(anyString());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With