mardi 29 septembre 2015

PowerMock and the class net.sf.ehcache.Cache

I am having bothering mocking the class net.sf.ehcache.Cache using PowerMock Here is the class I want to test

    package com.services.amazon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class CacheServices {

    /** Local log variable. **/
    private static final Logger LOG = LoggerFactory.getLogger(CacheServices.class);

    private CacheManager cacheManager;    

    public void setCacheManager(CacheManager argCacheManager) {
        cacheManager = argCacheManager;
    }


    public boolean replaceItemInCache(String cacheName, Object cacheKey, Object cacheObject) {

        boolean result = false;

        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {            
            return result;
        }

        int initialCacheSize1 = cache.getSize();

        Element retrievedCacheObject = cache.get(cacheKey);
        if (retrievedCacheObject != null) {
            LOG.info("Object exists for the cacheKey of {} - now removing from cache", cacheKey);
            boolean removeFromCacheResult = cache.remove(cacheKey);
        }

        int initialCacheSize2 = cache.getSize();        

        Element newCacheElement = new Element(cacheKey, cacheObject);
        cache.put(newCacheElement);
        int finalCacheSize = cache.getSize();

        Object[] logParams = new Object[]{initialCacheSize1, initialCacheSize2, finalCacheSize};
        LOG.info("initialCacheSize1:{}, initialCacheSize2:{}, finalCacheSize:{}", logParams);

        result = true;
        return result;
    }
}

And my test class

package com.services.amazon;

import static org.junit.Assert.assertTrue;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest({CacheServices.class, CacheManager.class, Cache.class, Element.class})
public class TestCacheServices {

    CacheServices cacheServices;
    CacheManager mockCacheManager;
    Cache mockCache;

    @Before
    public void setUp() {
        cacheServices = new CacheServices();
        mockCacheManager = PowerMock.createMock(CacheManager.class);
        mockCache = PowerMock.createMock(Cache.class);

        cacheServices.setCacheManager(mockCacheManager);
    }


    @Test
    public void testReplaceItemInCache_SuccessObjectExistsInCache() {    
        String cacheName = "cache";
        String cacheKey = "cache";
        Object cacheObject = new Object();

        Element dummyElement = new Element("Key", "value");

        EasyMock.expect(mockCacheManager.getCache(EasyMock.anyString())).andReturn(mockCache);        
        EasyMock.expect(mockCache.get(EasyMock.anyObject())).andReturn(dummyElement);
        EasyMock.expect(mockCache.getSize()).andReturn(1).atLeastOnce();
        EasyMock.expect(mockCache.remove(EasyMock.anyObject())).andReturn(true);
        mockCache.put(EasyMock.isA(Element.class));
        EasyMock.expectLastCall();

        EasyMock.replay(mockCacheManager);
        PowerMock.replay(mockCache);       

        boolean result = cacheServices.replaceItemInCache(cacheName, cacheKey, cacheObject);
        assertTrue("Expected True but was " + result, result);

        EasyMock.verify(mockCacheManager);
        PowerMock.verify(mockCache);
    }
}

The error console I get from running the test is as follows

java.lang.AssertionError: 
  Unexpected method call Cache.get("cache"):
    Cache.get(<any>): expected: 1, actual: 0
    Cache.remove(<any>): expected: 1, actual: 0
    Cache.put(isA(net.sf.ehcache.Element)): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl.invoke(EasyMockMethodInvocationControl.java:91)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:124)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:185)
    at net.sf.ehcache.Cache.get(Cache.java)
    at com.services.amazon.CacheServices.replaceItemInCache(CacheServices.java:48)
    at com.services.amazon.TestCacheServices.testReplaceItemInCache_SuccessObjectExistsInCache(TestCacheServices.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Can anyone point out to me what I may be doing wrong? I understand the Cache class is final and I have read the relevant documentation

Thanks Damien

Aucun commentaire:

Enregistrer un commentaire