title: junit @SpringBootTest for org.springframework.cache.Cache
body: <p>I already have spring ehcache in place in a spring-boot application. 1 api is implemented for cache eviction. And I am writing junits for this. </p>
  
  <p>Here is the config code of cacheManager in my Configuration.java </p>
  
  <pre><code> @Bean
      public CacheManager cacheManager()
      {
              EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
              factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
              factoryBean.setShared(true);
              return new EhCacheCacheManager(factoryBean.getObject());
      }
  </code></pre>
  
  <p>In my test class I am not able to mock CacheManager, as not able to read ehcache.xml. </p>
  
  <p>Here is my testClass:</p>
  
  <pre><code>import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.assertTrue;
  import static org.mockito.Mockito.when;
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.Mockito;
  import org.mockito.MockitoAnnotations;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.boot.test.context.TestConfiguration;
  import org.springframework.cache.CacheManager;
  import org.springframework.cache.ehcache.EhCacheCacheManager;
  import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.core.io.ClassPathResource;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.util.ReflectionTestUtils;
  
  
  
  @SpringBootTest
  @RunWith(SpringRunner.class)
  public class DriverServiceTest
  {
  
      @InjectMocks
      private DriverServiceImpl driverServiceImpl = null;
  
      @Mock
      private DriverMongoRepository mongoRepository;
  
      @Autowired
      private CacheManager ehCacheManager;
  
      @TestConfiguration
      public static class TestConfig
      {
          @Bean
          public CacheManager cacheManager()
          {
              EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
              factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
              factoryBean.setShared(true);
              return new EhCacheCacheManager(factoryBean.getObject());
          }
      }
  
      @Before
      public void setUp()
      {
          MockitoAnnotations.initMocks(this);
  
      }
  
      @Test
      public void testEvictCache() throws Exception
      {
        //Trying to mock ehCacheManager similar to cacheManager() of configuration.java
  
          String response = driverServiceImpl.evictCache("dummyUser", "getDriver", "1");
      }
  }
  </code></pre>
  
  <p>Since my ehCacheManager is not properly mocked, I get null pointer at 
  Cache cache =  ehCacheManager.getCache("getDriver");</p>
  
  <p>Can somebody please help me to mock CacheManager similar to cacheManager() of configuration.java</p>
  
  <p>Hope my question is clear!! 
  Thanks in Advance</p>