title: Controller Unit Test How do I mock RedisTemplate opsForValue with return type ValueOperations
body: <p>In my controller I have code like the following.
  RedisTemplate stringRedisTemplate</p>
  
  <pre><code>def accessRedis()
  {
     val = stringRedisTemplate.opsForValue().get('Key')
  }
  </code></pre>
  
  <p>In my controller Test, I intent to inject a mocked RedisTemplate that return a mocked ValueOperations. My code:</p>
  
  <pre><code>def template = mockFor(RedisTemplate)       
  def val = mockFor(org.springframework.data.redis.core.ValueOperations)
  val.demand.get {p-&gt; println "$p"}
  template.demand.opsForValue {
      return val.createMock()
  }
  
  controller.stringRedisTemplate = template.createMock()
  controller.accessRedis()
  </code></pre>
  
  <p>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'</p>
  
  <p>Could you advice a solution for my scenario? Thanks!</p>