首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.put方法存根模拟net.sf.ehcache.Cache (ehcache) (Mockito)

使用.put方法存根模拟net.sf.ehcache.Cache (ehcache) (Mockito)
EN

Stack Overflow用户
提问于 2012-05-25 03:06:04
回答 3查看 9.5K关注 0票数 8

我正在用EhCache开发一个简单的缓存功能。有一个实现缓存接口(BECache)的泛型基类:

代码语言:javascript
复制
public class EhCacheBase<K, V> implements BECache<K, V> {

private static CacheManager cacheManager;
private String cacheName;

public EhCacheBase(String cacheName) {
    this.cacheName = cacheName;
}

public void cache(K key, V value) {
    Cache cache = cacheManager.getCache(cacheName);
    if (cache == null) {
        throw new NullPointerException("Failed to obtain cache: " + cacheName);
    }
    Element element = new Element(key, value);
    cache.put(element);
}

其目的是具有如下定义的缓存:

代码语言:javascript
复制
public class ObjectCache extends EhCacheBase<String, Object>{

    public ObjectCache (String cacheName) {
       super(cacheName);
    }
}

假设在ehcache.xml中定义了'cacheName‘,我得到了一个很好的类来缓存某些对象。

我正在尝试为'EhCacheBase.cache(...)‘编写一个测试。确保检索缓存并放置一个值的方法(使用Mockito):

代码语言:javascript
复制
private static final String CACHE_NAME = "testCache";
@Mock
private CacheManager cacheManager;
@Mock
private Cache cache;
private EhCacheBase<String, Object> ehCacheBase;

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    ehCacheBase = new EhCacheBase<String, Object>(CACHE_NAME);
    EhCacheBase.setCacheManager(cacheManager);
}

@Test
public void shouldRetrieveCacheAndPutOneValueInIt() {
    //given
    Object o = new Object();
    when(cacheManager.getCache(CACHE_NAME)).thenReturn(cache);

    //when
    ehCacheBase.cache("KEY", o);

    //then
    verify(cacheManager, times(1)).getCache(CACHE_NAME);
    verify(cache, times(1)).put(any(Element.class));
}

当我运行测试时,我得到:

代码语言:javascript
复制
java.lang.NullPointerException
    at net.sf.ehcache.Cache.checkStatus(Cache.java:2731)
    at net.sf.ehcache.Cache.putInternal(Cache.java:1440)
    at net.sf.ehcache.Cache.put(Cache.java:1417)
    at net.sf.ehcache.Cache.put(Cache.java:1382)
    at org.fxoo.bookingengine.dao.dataproviders.caches.EhCacheBase.cache(EhCacheBase.java:24)
    at org.fxoo.bookingengine.dao.dataproviders.caches.EhCacheBaseTest.shouldCacheOneValue(EhCacheBaseTest.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
    at $Proxy0.invoke(Unknown Source)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

看起来'cache‘mock中的'put’方法正在调用其他一些方法。但是你可以在Mockito文档中读到:

使用doNothing()将空方法设置为不执行任何操作。注意,默认情况下,mock上的void方法什么也不做!但是,在极少数情况下,doNothing()会派上用场:

顺便说一句,我试过了: doNothing().when(cache).put(any(Element.class));,但它也没有用。

你知道是怎么回事吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-25 23:27:14

Cache.put是最终的,所以如果你想模拟它,你需要使用PowerMock

您的测试将如下所示:

代码语言:javascript
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest(Cache.class)
public class EhCacheBaseTest {
    private static final String CACHE_NAME = "testCache";
    private CacheManager cacheManager;
    private Cache cache;
    private EhCacheBase<String, Object> ehCacheBase;

    @Before
    public void init() {
        // You can statically import mock method, but left it this way 
        // for clarity
        cacheManager = PowerMockito.mock(CacheManager.class);
        cache = PowerMockito.mock(Cache.class);
        ehCacheBase = new EhCacheBase<String, Object>(CACHE_NAME);
        EhCacheBase.setCacheManager(cacheManager);
    }

    @Test
    public void shouldRetrieveCacheAndPutOneValueInIt() {
        //given
        Object o = new Object();
        when(cacheManager.getCache(CACHE_NAME)).thenReturn(cache);

        //when
        ehCacheBase.cache("KEY", o);

        //then
        verify(cacheManager, times(1)).getCache(CACHE_NAME);
        verify(cache, times(1)).put(any(Element.class));
    }
}
票数 11
EN

Stack Overflow用户

发布于 2012-05-25 16:02:34

我在Mockito Google Group (here)上发布了一个类似的问题。我得到了Eric Lefevre-Ardant的快速回复,解释了一切。net.sf.ehcache.Cache.put方法是最终的,所以它不能被存根-- Mockito未能提供它自己的实现。

票数 3
EN

Stack Overflow用户

发布于 2015-01-28 08:24:50

Mockito (1.10.19)的最新版本是not supported by PowerMock

如果可能,另一种解决方案是编写net.sf.ehcache.Ehcache接口代码,而不是net.sf.ehcache.Cache实现,而不是使用PowerMock。

Ehcache接口可以很容易地用标准的Mockito模拟。

我改编了被接受的答案的例子:

代码语言:javascript
复制
@RunWith(MockitoJUnitRunner.class)
public class EhCacheBaseTest {
    private static final String CACHE_NAME = "testCache";

    @Mock private Ehcache cache;
    @Mock private CacheManager cacheManager;

    private EhCacheBase<String, Object> ehCacheBase;

    @Before
    public void init() {
        ehCacheBase = new EhCacheBase<String, Object>(CACHE_NAME);
        EhCacheBase.setCacheManager(cacheManager);
    }

    @Test
    public void shouldRetrieveCacheAndPutOneValueInIt() {
        //given
        Object o = new Object();
        when(cacheManager.getEhcache(CACHE_NAME)).thenReturn(cache);

        //when
        ehCacheBase.cache("KEY", o);

        //then
        verify(cacheManager, times(1)).getEhcache(CACHE_NAME);
        verify(cache, times(1)).put(any(Element.class));
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10743772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档