Easymock: Missing behavior definition for the preceeding method call

Source: Internet
Author: User

If a method of a class which will be mocked by easymock did return some value, you should specify the return value explicitly before you use the mock object in your unit test case. or else easymock will complain:

 

Java. Lang. illegalstateexception: Missing behavior definition for the preceeding method callXxx

 

An example:

Say we have a interface named subject which has a member method as follows:

 

Public interface subject {<br/> callback addobserver (callback newobserver); <br/>}< br/> 

 

Then we shoshould use easymock to mock this interface in following way:

 

Public void testregisterobserver () throws exception {<br/> subject mocksubject = easymock. createnicemock (subject. class); <br/> callback mockcallback = easymock. createnicemock (callback. class); <br/> easymock. wrong CT (mocksubject. addobserver (easymock. <callback> notnull ())). andreturn (mockcallback); <br/> easymock. replay (mocksubject); <br/> // test code here... <br/> easymock. verify (mocksubject); <br/>} 

 

Note the line containsAndreturn. If you missed the call to andreturn, easymock will throw out the above illegalstateexception.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.