我有一个我要测试的课程RequestValidator
。此类方法validate()
将RequestData
作为方法参数,RequestData
是我的POJO类。
package com.ge.health.gam.contactge.service;
import org.springframework.stereotype.Component;
import com.ge.health.gam.common.validate.Validation;
import com.ge.health.gam.common.validate.ValidationResult;
import com.ge.health.gam.common.validate.Validator;
import com.ge.health.gam.contactge.model.RequestData;
@Component
public class RequestValidator implements Validator<RequestData> {
@Override
public ValidationResult validate(String objectName, RequestData rqdata) {
// TODO Auto-generated method stub
String regex = "^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2},([01]?[0-9]|2[0-3]):[0-5][0-9]$";
ValidationResult result = new ValidationResult();
if (rqdata == null) {
result.addError("error.invalidObjectGraph", "Object graph not initialized correctly");
return result;
}
Validation.rule("EquipmentId", rqdata.getEquipmentID()).required().run(result);
Validation.rule("Modality", rqdata.getModality()).required().run(result);
Validation.rule("FacilityID", rqdata.getFacilityID()).required().run(result);
Validation.rule("CountryCode", rqdata.getCountryCode()).required().maxLength(3).matches("^[a-zA-Z]*$")
.run(result);
Validation.rule("ProblemType", rqdata.getProblemType()).required().run(result);
Validation.rule("Name", rqdata.getName()).required().maxLength(20).run(result);
Validation.rule("PhoneNumber", rqdata.getPhoneNumber()).required().maxLength(25).matches("[0-9]+").run(result);
Validation.rule("Extension", rqdata.getExtension()).required().maxLength(10).matches("[0-9]+").run(result);
Validation.rule("Description", rqdata.getDescription()).required().maxLength(300).run(result);
Validation.rule("ShortDescription", rqdata.getShortDescription()).required().maxLength(80).run(result);
Validation.rule("TimePeriod", rqdata.getTimePeriod()).required().matches(regex).run(result);
Validation.rule("Locale", rqdata.getLocale()).required().run(result);
System.out.println("value of requesting app is:" + rqdata.getRequestingApp());
Validation.rule("RequestingApp", rqdata.getRequestingApp()).required().matches("icenter").run(result);
System.out.println(result.getErrorDetails());
return result;
}
}
这是我的Model类。
package com.ge.health.gam.contactge.model;
import org.springframework.format.annotation.DateTimeFormat;
public class RequestData {
private static final long serialVersionUID = 1L;
private String equipmentID;
private String modality;
private String facilityID;
private String countryCode;
private String isoCode;
private String problemType;
private String problemArea;
private String equipmentStatus;
private String name;
private String phoneNumber;
private String extension;
private String description;
private String shortDescription;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private String timePeriod;
private String serviceCode;
private String locale;
private String requestingApp;
private String examNumber;
private String seriesNumber;
private String imageNumber;
public String getImageNumber() {
return imageNumber;
}
public void setImageNumber(String imageNumber) {
this.imageNumber = imageNumber;
}
public String getEquipmentID() {
return equipmentID;
}
public void setEquipmentID(String equipmentID) {
this.equipmentID = equipmentID;
}
public String getModality() {
return modality;
}
public void setModality(String modality) {
this.modality = modality;
}
public String getFacilityID() {
return facilityID;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getIsoCode() {
return isoCode;
}
public void setIsoCode(String isoCode) {
this.isoCode = isoCode;
}
public String getProblemType() {
return problemType;
}
public void setProblemType(String problemType) {
this.problemType = problemType;
}
public String getProblemArea() {
return problemArea;
}
public void setProblemArea(String problemArea) {
this.problemArea = problemArea;
}
public String getEquipmentStatus() {
return equipmentStatus;
}
public void setEquipmentStatus(String equipmentStatus) {
this.equipmentStatus = equipmentStatus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getTimePeriod() {
return timePeriod;
}
public void setTimePeriod(String timePeriod) {
this.timePeriod = timePeriod;
}
public String getServiceCode() {
return serviceCode;
}
public void setServiceCode(String serviceCode) {
this.serviceCode = serviceCode;
}
public String getLocale() {
return locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
public String getRequestingApp() {
return requestingApp;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("RequestData [equipmentID=").append(equipmentID).append(", modality=").append(modality)
.append(", facilityID=").append(facilityID).append(", countryCode=").append(countryCode)
.append(", isoCode=").append(isoCode).append(", problemType=").append(problemType)
.append(", problemArea=").append(problemArea).append(", equipmentStatus=").append(equipmentStatus)
.append(", name=").append(name).append(", phoneNumber=").append(phoneNumber).append(", extension=")
.append(extension).append(", description=").append(description).append(", shortDescription=")
.append(shortDescription).append(", timePeriod=").append(timePeriod).append(", serviceCode=")
.append(serviceCode).append(", locale=").append(locale).append(", requestingApp=").append(requestingApp)
.append(", examNumber=").append(examNumber).append(", seriesNumber=").append(seriesNumber)
.append(", imageNumber=").append(imageNumber).append("]");
return builder.toString();
}
public void setRequestingApp(String requestingApp) {
this.requestingApp = requestingApp;
}
public String getExamNumber() {
return examNumber;
}
public void setExamNumber(String examNumber) {
this.examNumber = examNumber;
}
public String getSeriesNumber() {
return seriesNumber;
}
public void setSeriesNumber(String seriesNumber) {
this.seriesNumber = seriesNumber;
}
public void setFacilityID(String facilityID) {
this.facilityID = facilityID;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
这是我的考试班。
package com.ge.health.gam.contactge.service.Test;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import com.ge.health.gam.common.validate.ValidationResult;
import com.ge.health.gam.contactge.model.RequestData;
import com.ge.health.gam.contactge.service.RequestValidator;
import junit.framework.Assert;
public class RequestValidatorTest {
RequestValidator reqValidator = new RequestValidator();;
@Mock
RequestData reqData;
@Mock
ValidationResult validateResult;
@Before
public void setUp() throws Throwable {
MockitoAnnotations.initMocks(this);
}
@Test
@PowerMockIgnore
public void testValidate() {
SimplePojo pojo = new SimplePojo();
pojo.setCountryCode("123");
pojo.setEquipmentID("414541HD01");
pojo.setModality("sjdksd");
pojo.setFacilityID("sdjhsdj");
pojo.setCountryCode("CA");
pojo.setIsoCode("2");
pojo.setProblemType("ddddddddd");
pojo.setProblemArea("ssbnc");
pojo.setEquipmentStatus("sdsds");
pojo.setName("pinku");
pojo.setPhoneNumber("2999999999992");
pojo.setExtension("dnbsd");
pojo.setDescription("sdsjd");
pojo.setShortDescription("sjdks");
pojo.setTimePeriod("01/01/2011,13:00");
pojo.setLocale("en_US");
pojo.setRequestingApp("icenter");
when(reqData.getCountryCode()).thenReturn("CA");
Assert.assertEquals(true, reqValidator.validate("priyanka", pojo));
Assert.assertEquals("CA", reqData.getCountryCode());
}
class SimplePojo extends RequestData {
private static final long serialVersionUID = 1L;
}
}
运行jUnit
测试用例
java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava /郎/类;)Lorg /的Mockito /插件/ MockMaker $ TypeMockability; 在org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:26) 在org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21) 在org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167) 在org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161) 在org.mockito.internal.MockitoCore.mock(MockitoCore.java:54) 在org.mockito.Mockito.mock(Mockito.java:1449)