[JUnit] easymock usage Summary

Source: Internet
Author: User
Steps for easymock

 

1. Generate the mock Interface

 

  1. Iservice mockservice = easymock. createmock ("name", iservice. Class );

 

To use a mock object instead of an interface, use class Extension: org. easymock. classextension. easymock.

If you want multiple mock interfaces, you 'd better use mockcontrol to manage them:

 

  1. Imockscontrol control = easymock. createcontrol ();
  2. Iservice1 mockobj1 = control. createmock (iservice1.class );
  3. Iservice2 mockobj2 = control. createmock (iservice2.class );

 

 

2. Set the expected behavior

If the returned value is void:

 

  1. Mockservice. dovoidmethod ();
  2. Easymock. expectlastcall (); // you can ignore this sentence in the latest easymock version.


If an exception is required:

 

 

  1. Easymock. expectlastcall (). andthrow (
  2. New myexception (New runtimeexception (). anytimes ();


If the returned value is not void:

 

 

  1. Easymock. compute CT (mockservice. dosomething (ISA (long. Class), ISA (report. Class ),
  2. Isnull (report. Class). andreturn ("Return string"). anytimes ();

 

In the preceding example, three parameters are input: Long, report, and null. Note: If the parameter is of the basic type long, easymock. anylong ()

The input parameter can also be defined as a specific object rather than a class.

 

3. Switch the mock object to the replay state.

 

  1. Easymock. Replay (mockservice );

If mockcontrol is used for management:

  1. Control. Replay ();

 

4. Test

 

  1. Bo. setservice (mockservice );
  2. Bo. dosomething ();

 

5. Verify the behavior of the mock object

 

  1. Easymock. Verify (mockservice );

If mockcontrol is used for management:

  1. Control. Verify ();

 

 

CT () Considerations

 

When the input parameter is expected to be of the basic type

When you use objective CT to set the expected call method of the mock method, if you use the basic type, but do not use the value of the basic type,

Unavailable: easymock.Isa(Long. Class)

Use easymock.Anylong()

 

If the input parameter is expected to be null

If the input parameter may be null

 

  1. ISA (string. Class)

If the input is null, an error (ISA (Java. Lang. String), <Any>): expected: 1, actual: 0

Use:

 

  1. Or (ISA (string. Class), isnull ())

 

It can be determined only when the returned results are running.

It is very likely that the returned results of a method are not fixed. For example, the returned results vary according to the input parameters.Andanswer():

 

  1. Easymock.expect(mockservice.exe cute (easymock. anyint (). andanswer (New ianswer <integer> (){
  2. Public integer answer () throws throwable {
  3. Integer COUNT = (integer) easymock. getcurrentarguments () [0];
  4. Return count * 2;
  5. }
  6. });


Note: You can use easymock. getcurrentarguments () to obtain the input parameters!

 

 

Times ()

 

 

FAQ java. Lang. illegalstateexception: 2 matchers expected, 1 recorded.

It may be that when the mock method is set to the expected call method, both the ISA method is used to specify the parameter and a specific value is used as the parameter.

For example:

 

  1. Reverse CT (mockemployeerepository. findbydepartmentandspecification ("HR ",
  2. ISA (employeesearchspecification. Class). andreturn (emplooyees );


Correct syntax: -- use eq (specific value)

 

 

    1. Reverse CT (mockemployeerepository. findbydepartmentandspecification (eq ("HR "),
    2. ISA (employeesearchspecification. Class). andreturn (employees );

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.