title: Spring Boot @Cacheable for findOne
body: <p>I would like to add caching to my Spring Boot 4 application.
  As a key I would like to use id (type Long) of my entity, value is my entity.</p>
  <pre><code>@EnableCaching
  @SpringBootApplication
  public class SpringBootMainApplication {
    public static void main(String[] args) {
      SpringApplication.run(SpringBootMainApplication.class, args);
    }
  }
  
  @Configuration
  public class AppConfiguration {
    @Bean
    public CacheManager cacheManager() {
      return new ConcurrentMapCacheManager(&quot;findOne&quot;);
    }
  }
  
  public interface SampleEntityService extends CrudRepository&lt;SampleEntity, Long&gt; {
    @Cacheable(key = &quot;#a0&quot;, value = &quot;findOne&quot;)
    SampleEntity findOne(Long id);
  }
  </code></pre>
  <p>but when I call findOne method in my test I got an exception:</p>
  <pre><code>java.lang.ClassCastException: org.springframework.cache.support.SimpleValueWrapper cannot be cast to my.springboot.model.SampleEntity
    at my.springboot.SampleEntityCacheTest.aaa(SampleEntityCacheTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    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:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
  </code></pre>
  <p>According to some tutorials, I tried to use many variants, but all of them fail.</p>
  <pre><code>@Cacheable(key = &quot;#a0&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;#p0&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;a0&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;p0&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;#id&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;id&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;#root.id&quot;, value = &quot;findOne&quot;)
  @Cacheable(key = &quot;root.id&quot;, value = &quot;findOne&quot;)
  </code></pre>
  <p>What do I do wrong ?</p>
  <p>======= answer =========</p>
  <p>Proper solution is:</p>
  <pre><code>@Cacheable(key = &quot;#a0&quot;, value = &quot;findOne&quot;)
  </code></pre>
  <p>and inside the test I should have done something like that:</p>
  <pre><code>@Test
  public void aaa() {
      SampleEntity entity = sampleEntityService.save(create(&quot;name1&quot;, uuid(1)));
      sampleEntityService.findOne(entity.getId());
      SampleEntity entityFromCache =(SampleEntity) cacheManager.getCache(&quot;findOne&quot;).get(entity.getId()).get();
      assertNotNull(entityFromCache);
  }
  </code></pre>