EassyMock practices custom parameter matching, eassymock custom
Although easymock provides a large number of methods for Parameter Matching, in some special cases, for example, parameters are complex objects and cannot be simply compared using the equals () method, these existing parameter matchers are powerless. Easymock provides the IArgumentMatcher interface to implement the custom parameter matcher.
Let's talk about it using examples:
Interface to be tested
package MockTest;public interface Service { void execute(Request request, MData[] mdata, int mode);}
Parameter Type Definition
Package MockTest; public class Request {private boolean condition; private String value1; private String value2; public boolean isCondition () {return condition;} public String getValue1 () {return value1 ;} public String getValue2 () {return value2;} public void setCondition (boolean condition) {this. condition = condition;} public void setValue1 (String value1) {this. value1 = value1;} public void setValue2 (String value2) {this. value2 = value2;} public Request (boolean condition, String value1, String value2) {super (); this. condition = condition; this. value1 = value1; this. value2 = value2 ;}}View Codepackage MockTest; public class MData {public byte [] key; public byte [] data; public MData (byte [] key, byte [] data) {super (); this. key = key; this. data = data;} public String toString () {return "key:" + new String (key) + ", data:" + new String (data );}}View Code
Custom ID
In our separate test case, we have the following parameter matching logic: If condition is true, we only need to compare value1; if condition is false, you only need to compare value2. because the logic is inconsistent with the default equals method, we cannot directly use the equals method, but can only implement our own parameter matching.
package MockTest;import org.easymock.EasyMock;import org.easymock.IArgumentMatcher;public class RequestMatcher implements IArgumentMatcher { private boolean condition; private String expectedValue; private RequestMatcher(boolean condition, String expectedValue) { this.condition = condition; this.expectedValue = expectedValue; } @Override public void appendTo(StringBuffer buffer) { buffer.append("RequestMatcher expect(condition="); buffer.append(condition); buffer.append(" expectedValue="); buffer.append(expectedValue); buffer.append(")"); } @Override public boolean matches(Object argument) { if (!(argument instanceof Request)) { return false; } Request request = (Request) argument; if (condition) { return expectedValue.equals(request.getValue1()); } else { return expectedValue.equals(request.getValue2()); } } public static Request requestEquals(boolean condition, String expectedValue) { EasyMock.reportMatcher(new RequestMatcher(condition, expectedValue)); return null; }}
Parameter smdata is used to demonstrate how to implement parameter matching when the parameter is an Object array. The key is to forcibly convert the Object into an Object array.
Package MockTest; import org. easymock. easyMock; import org. easymock. IArgumentMatcher; // implements the IArgumentMatcher Interface class extends smdata implements IArgumentMatcher {private MData [] Reverse CT; private MData [] actual; public extends smdata (MData [] Reverse CT) {this. keep Ct = keep CT;} public static MData [] submit dataequals (MData [] Keep CT) {// submit the custom class EasyMock to match. reportMatcher (new Round smdata (round CT); return null ;}@ Overrid E // this method implements the logical public boolean matches (Object argument) matching parameters {// this method only can mathch one single parameter System. out. println ("argument is" + argument); // TODO Auto-generated method stub if (argument = this. except CT) return true; if (! (Argument instanceof MData []) return false; // matches does not provide a method to receive arrays. Therefore, you must forcibly convert OjectweiMData [] actual = (MData []) argument; int length = CT. length; if (length! = Actual. length) return false; for (int I = 0; I <length; I ++) {// if (CT [I]. key! = Actual [j]. key | CT [I]. data! = Actual [j]. data) // error if (! CT [I]. toString (). equals (actual [I]. toString () // if (! Arrays. equals (reverse CT, actual) // error {return false ;}} return true ;} @ Override // This method matches the information to be printed after an error. public void appendTo (StringBuffer buffer) {// TODO Auto-generated method stub buffer. append ("ipvsmput failed CT is: \ n"); for (int I = 0; I <distinct CT. length; I ++) {buffer. append (CT [I]. toString ();} buffer. append ("but actual is: \ n"); for (int j = 0; j <actual. length; j ++) {buffer. append (CT [j]. toString ());}}}
Test
Package MockTest; import org. easymock. *; import org. junit. *; import static org. easymock. easyMock. *; public class TestEasyMock {@ Test public void testConditionTrueFailure () {final boolean expectedCondition = true; final String expectedValue = "aaa"; Service service = EasyMock. createMock ("service", Service. class); MData [] datas = {new MData ("1001 ". getBytes (), "2001 ". getBytes (), new MData ("1002 ". getBytes (), "2002 ". getBytes (), new MData ("1003 ". getBytes (), "2003 ". getBytes ()}; Request request = new Request (expectedCondition, "aaa", "ccc ");
// The parameter matcher can only implement one parameter match at a time. Therefore, for multiple parameters, you must implement multiple custom matkers service.exe cute (RequestMatcher. requestEquals (expectedCondition, expectedValue), small smdata. optional dataequals (datas), anyInt (); EasyMock. expectLastCall (); EasyMock. replay (service); // MData [] datas2 = {new MData ("1001 ". getBytes (), "2001 ". getBytes ()}; service.exe cute (request, datas, 1); EasyMock. verify (service );}}