上述方法调用缺少行为定义:用法是:expect(a.foo())。和XXXX()

时间:2014-06-14 10:17:25

标签: java junit easymock

我是Junit的新手,我遇到了问题。任何帮助都会非常感激。

public void testGuaranteedRates() throws Exception
{

    ParticipantSummary summary = new ParticipantSummary();

    EasyMock.expect( iRequest.getPIN() ).andReturn( "1060720" );
    DateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
    Date date = new Date();
    EasyMock.expect( iRequest.getTradeDate() ).andReturn( date ).anyTimes();
    EasyMock.expect( control.prepareServiceRequest( iRequest ) ).andReturn( rtvint );
    EasyMock.replay();
    ems.replayAll();
}

方法prepareServiceRequest()如下所示

org.tiaa.transact.generated.jaxb.inquiry.RetrieveRetirementVintages prepareServiceRequest(InquiryRequest inquiryRequest)
{

    logger.debug( "prepareServiceRequest enter" );
    org.tiaa.transact.generated.jaxb.inquiry.ObjectFactory objectFactory = new org.tiaa.transact.generated.jaxb.inquiry.ObjectFactory();
    org.tiaa.transact.generated.jaxb.inquiry.RetrieveRetirementVintages retirementVintages = objectFactory.createRetrieveRetirementVintages();

    if( ( inquiryRequest ) != null )
    {
        if( ( inquiryRequest.getPIN() ) != null )
        {
            retirementVintages.setPIN( inquiryRequest.getPIN() );
        }
        if( ( inquiryRequest.getTradeDate() != null ) )
        {
            Calendar cal = new GregorianCalendar();
            //retirementVintages.setTradeDate( TPDateUtil.convertDatetoXMLGregorianCalendar( inquiryRequest.getTradeDate() ) );
            //retirementVintages.setTradeDate(( inquiryRequest.getTradeDate() );
        }
    }
    logger.debug( "prepareServiceRequest exit" );
    return retirementVintages;
}

当我尝试测试时,我收到如下错误

java.lang.IllegalStateException: missing behavior definition for the preceding method call: InquiryRequest.getPIN()

如果有任何问题,请有人告诉我。

2 个答案:

答案 0 :(得分:7)

假设iRequestcontrol是模拟对象,则需要重播它们。

所以,而不是:

EasyMock.replay();

试试这个:

EasyMock.replay(iRequest);
EasyMock.replay(control);

答案 1 :(得分:3)

您正在测试的方法中调用inquiryRequest.getPin()两次,但您只将模拟行为添加到一个调用中。所以,改为:

 EasyMock.expect( iRequest.getPIN() ).andReturn( "1060720" ).anyTimes(); 

或更改实现以将inquiryRequest.getPin()存储在变量中,可以让您更进一步。