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.

setup

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

use-as-source

now project should look like this

added as source

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

convert-to-maven

configure project with giving group id and artifact id

convertomave

implement

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


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.krishantha.sample.ehcache</groupId>
	<artifactId>EhCacheMock</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-all</artifactId>
			<version>1.9.5</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<version>2.9.0</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
	</dependencies>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

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);
		cache.put(element);
	}
}

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;

@RunWith(MockitoJUnitRunner.class)
public class EchCacheTest {

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

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

	@Before
	public void init() {
		cacheBase = new CacheBase&lt;&gt;(cacheName);
		CacheBase.setCacheManager(cacheManager);
	}

	@Test
	public void testFetchDataFromCache() {

		when(cacheManager.getEhcache(cacheName)).thenReturn(ehcache);
		// 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

run-as

test is pass !!

success-test

 

Share this on your world...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePrint this page

Leave a Reply