2015-09-29 4 views
0

Я имею утруждая насмешливый класс net.sf.ehcache.Cache с помощью PowerMock Вот класс, я хочу, чтобы проверитьPowerMock и класс net.sf.ehcache.Cache

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; 
    } 
} 

И мой тестовый класс

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); 
    } 
} 

консоль ошибок я получаю от выполнения теста заключается в следующем

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 кто-нибудь указывает мне, что я могу делать неправильно? Я понимаю, класс Cache является окончательным, и я прочитал соответствующую документацию

Благодаря Damien

+1

в 'CacheServices', является' кэша Manager.getCache (cacheName); 'return' null'? – thokuest

+0

его нет - он возвращает фиктивный экземпляр кэша, как ожидалось –

ответ

0

Получил этот фиксированный

Вместо использование объекта в качестве ключа - я заставил свой метод использовать строку как ключ (который нормален для моего приложения)

Тем не менее, если у кого есть какие-либо идеи о том, как сделать работу объекта в качестве ключа - это было бы оценен

Смежные вопросы