Java 缺少前面方法调用的行为定义:用法是:expect(a.foo()).andXXX()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24218875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
missing behavior definition for the preceding method call:Usage is: expect(a.foo()).andXXX()
提问by Btla Deva
I'm New to Junit and am stuck on an issue. Any help would be really appreciated.
我是 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();
}
The method prepareServiceRequest() is as below
方法 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;
}
When i tried to test it , I'm getting an error as below
当我尝试对其进行测试时,出现如下错误
java.lang.IllegalStateException: missing behavior definition for the preceding method call:
InquiryRequest.getPIN()
java.lang.IllegalStateException: missing behavior definition for the preceding method call:
InquiryRequest.getPIN()
Could anyone please let me know if anything is wrong here.
任何人都可以让我知道这里是否有任何问题。
回答by Louise Miller
You're calling inquiryRequest.getPin()
twice in the method you are testing, but you only add the mock behaviour to one call. So, changing to:
您在inquiryRequest.getPin()
正在测试的方法中调用了两次,但您只将模拟行为添加到一次调用中。所以,改为:
EasyMock.expect( iRequest.getPIN() ).andReturn( "1060720" ).anyTimes();
or changing the implementation to store the inquiryRequest.getPin()
in a variable, should get you further.
或更改实现以将其存储inquiryRequest.getPin()
在变量中,应该会让您走得更远。
回答by mokarakaya
Assuming that iRequest
and control
are mock objects, you need to replay them.
假设iRequest
和control
是模拟对象,您需要重放它们。
So, instead of:
所以,而不是:
EasyMock.replay();
try this:
尝试这个:
EasyMock.replay(iRequest);
EasyMock.replay(control);