我收到PowerMock错误,但我没有使用PowerMocking。我正在使用正常的嘲弄。这是我要测试的课程:
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注入模拟。
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());
}
}
我收到此错误:
java.lang.AbstractMethodError:org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/MockMaker$TypeMockability;atorg.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:26)atorg.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
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)
答案 0 :(得分:0)
我想 var data_str='[{"totalstaff":"2","totaltasks":"2","is_completed":"1"}]';
var data=JSON.parse(data_str);
if(data[0]){ //if length of data > 0
var table=$('<table/>').attr('border', '1');
var first_tr=$('<tr/>').css('text-align', 'center');
var td=$('<td/>').text('totalstaff');
first_tr.append(td);
td=$('<td/>').text('totaltasks');
first_tr.append(td);
td=$('<td/>').text('completed');
first_tr.append(td);
td=$('<td/>').text('pending');
first_tr.append(td);
table.append(first_tr);
for(var key in data){
var tr=$('<tr/>');
td=$('<td/>').text(data[key].totalstaff);
tr.append(td);
td=$('<td/>').text(data[key].totaltasks);
tr.append(td);
td=$('<td/>').text(data[key].is_completed);
tr.append(td);
td=$('<td/>').text(parseInt(data[key].totaltasks, 10) - parseInt(data[key].is_completed, 10));
tr.append(td);
table.append(tr);
}
$('body').append(table);
}
是最后一堂课。 Mockito 1.x本身无法模拟最终类,并且可能尝试通过包含PowerMock来模拟它们(您尚未安装)。
要解决您的问题,您可以至少升级到Mockito 2.1.0并启用support for mocking final classes and methods。如果您需要使用旧版本,请不要模拟最终课程,或者(如果您真的没有其他选择)尝试PowerMock。