gpt4 book ai didi

java - JUnit 5 测试中模拟 EhCache NullPointerException

转载 作者:行者123 更新时间:2023-11-30 05:44:06 24 4
gpt4 key购买 nike

我正在为我想要测试的服务编写单元测试。有几种方法尝试从 EhCache 检索值。我尝试用 Mockito 来模拟它们,并且只需让 Cacheget(String key) 方法返回 null,因为我想忽略缓存这些测试。

我的测试类:

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 类中:

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 在调用时返回 null,是否有任何简单的解决方案可以解决此问题?

最佳答案

如果没有一些配置,

Mockito 无法模拟 final 方法和类。正如 Morfic 指出的那样,Mockito v2.x 是可能的,如 here 所解释的那样。和 here .

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

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

关于java - JUnit 5 测试中模拟 EhCache NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55140945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com