gpt4 book ai didi

java - 获取 PowerMock 错误 : java. lang.AbstractMethodError : org. powermock.api.mockito.internal.mockmaker.PowerMockMaker

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:18 26 4
gpt4 key购买 nike

我收到 PowerMock 错误,但我没有使用 PowerMocking。我正在使用正常的模拟。这是我要测试的类(class):

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class IlinqBeanDataBuilder {

@Autowired
private Settings settings;

private static final String SERVICE_CODE_FOR_FACILITY_COUNTRY = "SERVICE_CODE_FOR_FACILITY_COUNTRY";
private static final String SERVICE_CODE_A_CONSTANT = "A";

private static final String HARD_DOWN = "HARD DOWN";
private static final String QUICK_QUESTION = "QUICK QUESTION";
private static final String PARTIAL_DOWN = "PARTIAL DOWN";

private static final String COMPLETELY_DOWN = "Completely Down";
private static final String UP = "Up";
private static final String PARTIALLY_DOWN = "Partially Down";

public RfsData prepareIlinqData(RequestData requestdata) throws FileNotFoundException {

System.out.println("M into prepare illinq data");
RfsData iLinkDataBean = new RfsData();
iLinkDataBean.setCountryCode(requestdata.getCountryCode());
iLinkDataBean.setSystemId(requestdata.getEquipmentID());
iLinkDataBean.setModality(requestdata.getModality());
/**
* Removing the US from FacilityID In future fix need to change based on
* some related information
*/
iLinkDataBean.setCustomerId(requestdata.getFacilityID().substring(3));

iLinkDataBean.setFirstname(requestdata.getName());
iLinkDataBean.setPnumber(requestdata.getPhoneNumber());
iLinkDataBean.setExtension(requestdata.getExtension());
iLinkDataBean.setDescription(requestdata.getDescription());
iLinkDataBean.setShortDescription(requestdata.getShortDescription());
iLinkDataBean.setPeriod(requestdata.getTimePeriod());
iLinkDataBean.setIlinq_local(requestdata.getLocale());
iLinkDataBean.setRequestApp(requestdata.getRequestingApp());
iLinkDataBean.setExamNum(requestdata.getExamNumber());
iLinkDataBean.setSeriesNum(requestdata.getSeriesNumber());
iLinkDataBean.setImageNum(requestdata.getImageNumber());
iLinkDataBean.setRequestorSystemId(requestdata.getEquipmentID());
iLinkDataBean.setServicecode(requestdata.getServiceCode());
iLinkDataBean.setIsocode(requestdata.getIsoCode());
iLinkDataBean.setProblemtype(requestdata.getProblemType());
if (StringUtils.isNotBlank(requestdata.getEquipmentStatus())
|| StringUtils.isNotEmpty(requestdata.getEquipmentStatus())) {
if (requestdata.getEquipmentStatus().trim().equalsIgnoreCase(HARD_DOWN)) {
iLinkDataBean.setStatus(COMPLETELY_DOWN);
} else if (requestdata.getEquipmentStatus().trim().equalsIgnoreCase(QUICK_QUESTION)) {
iLinkDataBean.setStatus(UP);
} else if (requestdata.getEquipmentStatus().trim().equalsIgnoreCase(PARTIAL_DOWN)) {
iLinkDataBean.setStatus(PARTIALLY_DOWN);
} else {
iLinkDataBean.setStatus(requestdata.getEquipmentStatus());
}
}

String CountryCode = iLinkDataBean.getCountryCode();
System.out.println("Hi country code is:" + CountryCode);

String list1 = this.settings.getString("contactge.americas.country.codes");
System.out.println("list 1 data is:" + list1);

String list2 = this.settings.getString("contactge.asia.country.codes");
String list3 = this.settings.getString("contactge.europe.country.codes");
String list4 = this.settings.getString("contactge.japan.country.codes");
List<String> RegionList1 = Arrays.asList((list1.split(",")));
List<String> RegionList2 = Arrays.asList((list2.split(",")));
List<String> RegionList3 = Arrays.asList((list3.split(",")));
List<String> RegionList4 = Arrays.asList((list4.split(",")));

System.out.println("List1 data is:" + RegionList1);
System.out.println("List2 data is:" + RegionList2);
System.out.println("List3 data is:" + RegionList3);
System.out.println("List4 data is:" + RegionList4);
String RegionCode = null;
if (RegionList1.contains(CountryCode)) {
System.out.println("M in list 1");

RegionCode = "Americas";

} else if (RegionList2.contains(CountryCode)) {
System.out.println("M in list 2");
RegionCode = "ROA";
} else if (RegionList3.contains(CountryCode)) {
System.out.println("M in list 3");
RegionCode = "EMEA";
} else if (RegionList4.contains(CountryCode)) {
System.out.println("M in list 4");
RegionCode = "Japan";
}

System.out.println("Region code:" + RegionCode);
iLinkDataBean.setRegionCode(RegionCode);
System.out.println("ilinq data bean builder data:" + iLinkDataBean);

// Getting regionCode from CountryCode using property file
// iLinkDataBean.setRegionCode(RegionCode);
System.out.println("system id----------------:" + iLinkDataBean.getSystemId());

System.out.println("ilinq data bean builder data:" + iLinkDataBean);
return iLinkDataBean;
}
}

这是我的测试课。在此,我正在模拟从数据库中获取数据的设置对象,并且正在 ILinqBeanDataBuilder 中进行注入(inject)模拟。

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;

import junit.framework.Assert;

@RunWith(MockitoJUnitRunner.class)
public class IlinqBeanDataBuilderTest {

@InjectMocks
IlinqBeanDataBuilder iLinqdata;

@Mock
private Settings settings;

@Before
public void setUp() throws Throwable {

MockitoAnnotations.initMocks(this);

// iLinqdata = new IlinqBeanDataBuilder();

}

@Test
public void testCheckSum() throws FileNotFoundException {

RequestData reqData = new RequestData();
reqData.setCountryCode("123");
reqData.setEquipmentID("414541HD01");
reqData.setModality("sjdksd");
reqData.setFacilityID("sdjhsdj");
reqData.setCountryCode("CA");
reqData.setIsoCode("2");
reqData.setProblemType("ddddddddd");
reqData.setProblemArea("ssbnc");
reqData.setEquipmentStatus("sdsds");
reqData.setName("pinku");
reqData.setPhoneNumber("2999999999992");
reqData.setExamNumber("12323");
reqData.setServiceCode("sdfsfdf");
reqData.setExtension("3473437");
reqData.setDescription("sdsjd");
reqData.setShortDescription("sjdks");
reqData.setTimePeriod("01/01/2011,13:00");
reqData.setLocale("en_US");
reqData.setRequestingApp("icenter");
reqData.setImageNumber("sdjsh");
reqData.setSeriesNumber("2334");
Mockito.when(settings.getString("contactge.americas.country.codes"))
.thenReturn("CA,CL,MX,PR,US,AR,BR,GD,VE,CR,PE,AS,DO,HN,PA,CO,BO,GT,JM,EC,PY,UY,NI,SV");
Mockito.when(settings.getString("contactge.asia.country.codes"))
.thenReturn("IN,CN,AU,HK,KR,MY,SG,TW,TH,ID,PH,NZ,VN,C3,H2,DV");

Mockito.when(settings.getString("contactge.europe.country.codes")).thenReturn(
"AT,BE,CH,DE,ES,FI,FR,GB,IT,NL,PT,SE,UK,NO,DK,PL,RO,ZA,IE,SA,EG,HU,TR,GR,CZ,IL,AE,AL,BG,CS,CY,DZ,IS,JO,KE,KW,LB,LU,MT,MC,MA,RE,RU,SI,SK,TN,UM,AZ,BA,BJ,BY,DI,GE,GF,HR,KZ,LT,LV,MK,UA,YU,OT,NG");
Mockito.when(settings.getString("contactge.japan.country.codes")).thenReturn("JP");

RfsData rfs = iLinqdata.prepareIlinqData(reqData);
System.out.println(rfs.getCountryCode());

Assert.assertEquals("CA", rfs.getCountryCode());
}
}

我收到这个错误:

            at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:54)
at org.mockito.Mockito.mock(Mockito.java:1449)

最佳答案

我猜 Settings 是最后一个类。 Mockito 1.x 本身无法模拟最终类,并且可能会尝试通过包含您尚未安装的 PowerMock 来模拟它们。

要解决您的问题,您至少可以升级到 Mockito 2.1.0 并启用 support for mocking final classes and methods .如果您需要坚持使用旧版本,要么不要模拟最终类,要么(如果您真的别无选择)试试 PowerMock。

关于java - 获取 PowerMock 错误 : java. lang.AbstractMethodError : org. powermock.api.mockito.internal.mockmaker.PowerMockMaker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36405708/

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