title: Junit for Spring Cache Manager
body: <p>I am using spring caching layer in my application and I have got an issue as part of writing unit tests to test spring cache layer using <strong>Mockito</strong>.</p>
  
  <p>Please refer the below code for my problem:</p>
  
  <p><strong>Service layer:</strong></p>
  
  <pre><code>    public CustomerServiceImpl implements CustomerService {
      @Autowired
      private CacheManager cacheManager;
  
      @Autowired
      private CustomerRepository customerRepository;//Repository is simple JPA repository interface which contains findByCustomerName()
  
      @Override
      @CachePut(value = "#customer", key = "#customer.customerName")
      public Customer insertOrUpdate(Customer customer) {
          return customerRepository.save(customer);
      }
  
      @Cacheable(value="customersCache", key = "#customerName")
      public Customer findByCustomerName(String customerName) {
          Customer customer = customerRepository.findByCustomerName(customerName);
          return customer;
      }
  }
  </code></pre>
  
  <p><strong>JUnit test Code for Service layer:</strong></p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest(CustomerServiceImplTest.class)
  public class CustomerServiceImplTest {
  
      @Spy
      CacheManager cacheManager = new ConcurrentMapCacheManager("customersCache");
  
      @Mock
      CustomerRepository CustomerRepository;
  
      @InjectMocks
      CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void testCacheForFindByCustomerName() {
          Customer customer1 = new Customer();
          customer1.setId("1");
          customer1.setName("John");
          Customer customer2 = new Customer();
          customer2.setId("2");
          customer2.setName("Sara");
  
          //this should save to cache   
          Mockito.when(CustomerRepository.save(customer1))
          .thenReturn(customer1);
          customerServiceImpl.insertOrUpdate(customer1);
  
          //Now it should retreive from cache, but not able to
        Mockito.when(CustomerRepository.findByCustomerName(Mockito.any(String.class)))
                  .thenReturn(customer1, customer2);
  
          Customer result = customerServiceImpl.findByCustomerName("John");
          assertThat(result, is(customer1));
  
          result = customerServiceImpl.findByCustomerName("John");
          assertThat(result, is(customer1));
      }
  }
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <p>I am getting an "<code>java.lang.AssertionError:</code>" because the caching layer did not work and the call has been passed to repository object (twice) which has returned the 'customer2' mock object above i.e., the repository method has been invoked twice for the same key by passing the service layer.</p>
  
  <p>Also, please note that I am using "Mockito" framework for my tests.</p>
  
  <p>I have tried to google for unit tests on spring caching and also referred the below URL, which is almost using the same concept, but it does not work for my above code.</p>
  
  <p><a href="https://stackoverflow.com/questions/24221569/how-to-test-springs-declarative-caching-support-on-spring-data-repositories">How to test Spring&#39;s declarative caching support on Spring Data repositories?</a></p>
  
  <p>Could you please help to resolve the above exception ?</p>