JUnit 5 测试中的模拟 EhCache NullPointerException

Mocked EhCache NullPointerException in JUnit 5 test

我正在为我想测试的服务编写单元测试。有几种方法尝试从 EhCache 中检索值。 我尝试用 Mockito 模拟它们,并简单地将 Cache return 的 get(String key) 方法设置为 null,因为我想忽略这些测试的缓存。

我的测试class:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.jysk.dbl.esldataservice.model.Preis;
import com.jysk.dbl.esldataservice.service.PreisService;
import com.jysk.dbl.esldataservice.service.external.PimDataService;
import com.jysk.dbl.esldataservice.service.external.SapCarService;

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

public class PreisServiceTest {

    @Mock
    private SapCarService sapCarService;
    @Mock
    private ArticleDataService articleDataService;
    @Mock
    private CacheManager cacheManager;
    @Mock
    private Cache cache;

    @InjectMocks
    @Resource
    private PreisService preisService;

    @BeforeEach
    public void setup() {

        MockitoAnnotations.initMocks(this);

        when(this.cacheManager.getCache(anyString())).thenReturn(this.cache);
        when(this.cache.get(anyString())).then(null);
    }

    protected static final String TEST_STORE_IDENTIFIER = "1234";
    private static final String ARTICLE_IDENTIFIER_1 = "12345001";
    private static final String ARTICLE_IDENTIFIER_2 = "54321001";

    private final Preis p1 = new Preis(ARTICLE_IDENTIFIER_1, 10.00, 15.00, "01", "01", "01");
    private final Preis p2 = new Preis(ARTICLE_IDENTIFIER_2, 20.00, 25.00, "02", "02", "02");

    @Test
    void testGetPreisReturnsOneCorrectPreis() {

        when(this.sapCarService.getPreise(Arrays.asList(ARTICLE_IDENTIFIER_1), TEST_STORE_IDENTIFIER, true)).thenReturn(Arrays.asList(this.p1));

        final List<Preis> actual = this.preisService.getPreis(ARTICLE_IDENTIFIER_1, TEST_STORE_IDENTIFIER);

        verify(this.sapCarService, times(1)).getPreise(anyList(), anyString(), anyBoolean());

        assertNotNull(actual);
        assertEquals(1, actual.size());
        assertEquals(this.p1, actual);
    }
}

我的实现:

private Preis searchPreisInCache(String key) {

    final Element preisOptional = this.cacheManager.getCache("preis").get(key); // NPE here
    if (preisOptional != null) {

        final Preis preis = (Preis) preisOptional.getObjectValue();
        logger.info(String.format("Preis with key '%s' found in cache 'preis'.", key));
        return preis;
    }
    return null;
}

stackTrace 显示,NPE 被抛入 net.sf.ehcache.Cache class:

public final Element get(Object key) throws IllegalStateException, CacheException {
    getObserver.begin(); // NPE thrown here
    checkStatus();

    if (disabled) {
        getObserver.end(GetOutcome.MISS_NOT_FOUND);
        return null;
    }

    Element element = compoundStore.get(key);
    if (element == null) {
        getObserver.end(GetOutcome.MISS_NOT_FOUND);
        return null;
    } else if (isExpired(element)) {
        tryRemoveImmediately(key, true);
        getObserver.end(GetOutcome.MISS_EXPIRED);
        return null;
    } else if (!skipUpdateAccessStatistics(element)) {
        element.updateAccessStatistics();
    }
    getObserver.end(GetOutcome.HIT);
    return element;
}

这个问题有什么简单的解决方案吗,如果我只是想让 Cache 到 return null,每当它被调用时?

Mockito 不能模拟 final 方法和 类 没有一些配置。正如 Morfic 指出的那样,Mockito v2.x 是可行的,就像 here and here.

解释的那样

基本上,你必须在src/test/resources/mockito-extensions目录下添加一个名为org.mockito.plugins.MockMaker的文本文件,内容为mock-maker-inline和tada,Mockito可以模拟final方法和类.

但是,这使用具有不同限制的不同引擎,因此请注意这一点。