Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

Controller Unit Test How do I mock RedisTemplate opsForValue with return type ValueOperations

$
0
0

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!


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>