title: How to test Spring @Cacheable using customized key with SpEL
body: <p>I was looking for a way to test some of my @Cacheable and @CacheEvict methods and I've found out a very good explanation <a href="https://stackoverflow.com/a/24229350/4534078">here</a>.</p>
  
  <p>However, my case is a bit different and trickier.</p>
  
  <p>So, using the code from that post as example, I've added a cache key using spEL:</p>
  
  <pre><code>  interface MyRepo {
      @Cacheable(value = "sample", key = "#email.concat('.').concat(#email)")
      Object findByEmail(String email);
    }
  </code></pre>
  
  <p>Now, when trying to use Mockito.when(...):</p>
  
  <pre><code>  Mockito
      .when(repo.findByEmail(Mockito.any(String.class)))
      .thenReturn(first, second);
  </code></pre>
  
  <p>I got this spEl exception:</p>
  
  <pre><code>org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method concat(java.lang.String) on null context object
      at org.springframework.expression.spel.ast.MethodReference.throwIfNotNullSafe(MethodReference.java:144)
      at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:95)
      at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:85)
      at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
      at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
      at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
      at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
      at org.springframework.cache.interceptor.CacheOperationExpressionEvaluator.key(CacheOperationExpressionEvaluator.java:117)
      at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:742)
      at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:558)
      at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:502)
      at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:389)
      at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
      at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
      at techne.cronos.$Proxy36.findByEmail(Unknown Source)
      at techne.cronos.CachingIntegrationTest.methodInvocationShouldBeCached(CachingIntegrationTest.java:65)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
  </code></pre>
  
  <p>Does anyone have some guidance on how to pass through this testing issue?</p>