Steps for easymock 
 
 
1. Generate the mock Interface 
 
 
 
 - 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:
 
 
 
 
 - Imockscontrol control = easymock. createcontrol ();
  
 - Iservice1 mockobj1 = control. createmock (iservice1.class );
  
 - Iservice2 mockobj2 = control. createmock (iservice2.class );
  
 
 
 
 
 
2. Set the expected behavior 
If the returned value is void:
 
 
 
 
 - Mockservice. dovoidmethod ();
  
 - Easymock. expectlastcall (); // you can ignore this sentence in the latest easymock version.
  
 
If an exception is required:
 
 
 
 
 
 
 - Easymock. expectlastcall (). andthrow (
  
 - New myexception (New runtimeexception (). anytimes ();
  
 
If the returned value is not void:
 
 
 
 
 
 
 - Easymock. compute CT (mockservice. dosomething (ISA (long. Class), ISA (report. Class ),
  
 - 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. 
 
 
 
 - Easymock. Replay (mockservice );
  
 
If mockcontrol is used for management:
 
 
 - Control. Replay ();
  
 
 
 
4. Test 
 
 
 
 - Bo. setservice (mockservice );
  
 - Bo. dosomething ();
  
 
 
 
5. Verify the behavior of the mock object 
 
 
 
 - Easymock. Verify (mockservice );
  
 
If mockcontrol is used for management:
 
 
 - 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
 
 
 
 
 - ISA (string. Class)
  
 
If the input is null, an error (ISA (Java. Lang. String), <Any>): expected: 1, actual: 0
 
Use:
 
 
 
 
 - 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():
 
 
 
 
 - Easymock.expect(mockservice.exe cute (easymock. anyint (). andanswer (New ianswer <integer> (){
  
 - Public integer answer () throws throwable {
  
 - Integer COUNT = (integer) easymock. getcurrentarguments () [0];
  
 - Return count * 2;
  
 - }
  
 - });
  
 
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:
 
 
 
 
 - Reverse CT (mockemployeerepository. findbydepartmentandspecification ("HR ",
  
 - ISA (employeesearchspecification. Class). andreturn (emplooyees );
  
 
Correct syntax: -- use eq (specific value)
 
 
 
 
 
 
  
  - Reverse CT (mockemployeerepository. findbydepartmentandspecification (eq ("HR "),
  
  - ISA (employeesearchspecification. Class). andreturn (employees );