How to mock ehCache using mockito

Cache is something witch depend on in put and operation. also its has dependent operation. for example you have to write in to cache in same program in order to read from cache. this become really problematic when you trying to write unit test cases for cache methods using mock object. because you are mock cache objects mean it is not exist in cache. so your attempt to real will be fail.

this example will demonstrate how you can overcome this issue. this is eclipse based maven project. by following below instruction you can create project on your environment.


open eclipse and create new java project

Create new project in eclipse
Create new project in eclipse


give the project name as you desired. (Hello world is just a name. suggest EhCacheMockExample)

create new project

setup maven nature

this example based on eclipse mars which has maven in build plugin.

right click on src directory -> build path -> remove from build path

remove from build path

create src/main/java and src/test/java directories. (if you not understand maven nature refer this article)

create new folder

after create directories it will look like this

create maven folders

right click on src/main/java directory and build path -> use as source folder. so the same thing for main/test/java


now project should look like this

added as source

right click on project -> configure -> convert to maven project


configure project with giving group id and artifact id



add following dependancies to pom file and pom file should as follows. (goup id and artifact id based on your project name)

<project xmlns="" xmlns:xsi=""

create new class inside src/main/java and add implement as follows

package com.krishantha.sample.ehcache.core;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

public class CacheBase<K, V> {

	private static CacheManager cacheManager;

	public static void setCacheManager(CacheManager cacheManager) {
		CacheBase.cacheManager = cacheManager;

	private String cacheName;

	public CacheBase(String cacheName) {
		this.cacheName = cacheName;

	public void cache(K key, V value) {
		Ehcache cache = cacheManager.getEhcache(cacheName);
		if (cache == null) {
			throw new RuntimeException("Cannot get cache from cache manager");
		Element element = new Element(key, value);

create new class on src/test/java and implement as follows

package com.krishantha.test.ehcache;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.krishantha.sample.ehcache.core.CacheBase;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

public class EchCacheTest {

	private final String cacheName = "sample";
	private Ehcache ehcache;
	private CacheManager cacheManager;

	private CacheBase&amp;lt;String, Object&amp;gt; cacheBase;

	public void init() {
		cacheBase = new CacheBase&lt;&gt;(cacheName);

	public void testFetchDataFromCache() {

		// create cache
		Map&lt;String, String&gt;; student = new HashMap&lt;&gt;();
		student.put("Saman", "Sri Lanka");
		student.put("Kamal", "Sri Lanka");
		cacheBase.cache("team", student);

		verify(cacheManager, times(1)).getEhcache(cacheName);
		verify(ehcache, times(1)).put(any(Element.class));



now  run the test


test is pass !!



Share this on your world...Share on Facebook
Tweet about this on Twitter
Share on LinkedIn
Email this to someone
Print this page

Leave a Reply