Powermock 或 EasyMock 联合体

Powermock or EasyMock junits

我无法为此方法编写简单的测试class。

public void buildOBXSegmentForSeriesInfo( ImmunizationData immunizationData, int obxSetId , int observationSubId) throws HL7Exception {
        obx.getSetIDOBX().setValue(String.valueOf(obxSetId));
        obx.getValueType().setValue("CE");
        obx.getObservationIdentifier().getIdentifier().setValue("59783-1");
        obx.getObservationIdentifier().getText().setValue("Status in immunization series");
        obx.getObservationIdentifier().getNameOfCodingSystem().setValue("LN");
        obx.getObservationSubID().setValue(String.valueOf(immunizationData.getSeries().toString()));
        obx.getObservationResultStatus().setValue("F");
        System.out.println("Hellooo"+obx.encode());
    }

目前我有一个虚拟免疫数据xml。 class 名字是 "OBXSegmentBuilder"。 "OBX" 是第三方 jar。

我写了下面的测试 class 但失败了:

public void buildOBXSegmentForSeriesInfoTest() throws Exception {
        final OBX obx=PowerMock.createMock(OBX.class);
        //OBXSegmentBuilder OBXSegmentBuilder=EasyMock.createMock(OBXSegmentBuilder.class);
        OBXSegmentBuilder OBXSegmentBuilder1=PowerMock.createMock(OBXSegmentBuilder.class);

        URL inputFile = this.getClass().getResource("immunizationData.xml");        
        ImmunizationData immunizationData = null;
        immunizationData = JAXBUtilities.unmarshal(ImmunizationData.class, inputFile);
        PowerMock.replay(OBXSegmentBuilder1);
        final int FIVE=5;
        OBXSegmentBuilder1.obx=obx;
        PowerMock.expectPrivate(OBXSegmentBuilder1, "buildOBXSegmentForSeriesInfo",immunizationData, FIVE, FIVE).andReturn(null);

//      OBXSegmentBuilder.buildOBXSegmentForSeriesInfo(immunizationData, FIVE, FIVE);
        System.out.println(OBXSegmentBuilder1.obx.encode());



        }

异常:

java.lang.AssertionError: 
  Unexpected method call OBXSegmentBuilder.buildOBXSegmentForSeriesInfo(ImmunizationData [immunizationID=1, immunizationGroupId=1, inactive=N, personID=1346775390650600, secondaryDocumentID=1394369211250690, vaccineGroupName=null, vaccineName=IPV, medicalDisplayName=IPV, series=1, wasGiven=Y, reasonNotGiven=null, historical=N, historicalSource=N, vfcEligibility=0, drugDescID=123, drugNameID=789, genericProductIndex=17345, knowledgeBaseDrugCode=137, ndcCode=125, cvxCode=89, administeredDose=0.5, administeredDoseUnits=mL, route=Intramuscular, routeCode=IM, site=Left Thigh, siteCode=LT, manufacturer=Sanofi Pasteur, manufacturerCode=SPF, lotNumber=123, expirationDate=Sun Oct 12 00:00:00 UTC 2014, visPublishedDate=Wed Oct 12 00:00:00 UTC 2011, administeredByPVId=1363787536000611, administeredDate=Wed Oct 12 00:00:00 UTC 2011, administeredComments=null, advReactionDateTime=null, advReactionComments=null, advReactionCmtByPVId=null, isSigned=null, signedByPVId=1363787536000610, signedDate=null, filedInError=N, reasonRemoved=null, stopdate=null, dbCreateDate=Sun May 05 20:27:05 UTC 2013, createdBy=1363787536000610, dbUpdatedDate=Sun May 05 20:27:05 UTC 2013, lastModifiedBy=1363787536000610, administeredDateType=null, advReactionDateTimeType=null, reasonNotGivenMedical=null, reasonNotGivenMedicalDetail=null, administeredDoseWasted=null, visGiven=null, visGivenDate=null, fundingSource=null, administeredByName=null, advReactionCmtByName=null, signedByName=null, allergyGroupId=null, signedbyUserDetail=null, administeredbyUserDetail=null, createdbyUserDetail=null, documentDetail=null, allergyDetail=null, medicationInfo=null, isCombo=null, vfcEligibilityDesc =null, fundingSourceDesc =null, reasonNotGivenMedicalDetailDesc =null], 5, 5):
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:85)
    at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:94)
    at com.gehcit.cp.hl7.v25.segment.builder.OBXSegmentBuilder$$EnhancerByCGLIB$4c52dd.buildOBXSegmentForSeriesInfo(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1819)
    at org.powermock.api.easymock.PowerMock.doExpectPrivate(PowerMock.java:2034)
    at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1223)
    at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1234)
    at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1180)
    at com.gehcit.cp.hl7.v25.segment.builder.OBXSegmentBuilderTest.buildOBXSegmentForSeriesInfoTest(OBXSegmentBuilderTest.java:35)

看起来还不错,但匹配取决于 ImmunizationData.equals 的行为。由于它不匹配,您可以确信 equals 返回了 false

我建议升级您的 EasyMock 版本。您会收到一条更好的消息,告诉您当前的录音,以便更容易知道哪里出了问题。