雖然easymock中提供了大量的方法來進行參數匹配,但是對于一些特殊場合比如參數是復雜對象而又不能簡單的通過equals()方法來比較,這些現有的參數匹配器就無能為力了。easymock為此提供了IArgumentMatcher 接口來讓我們實現自定義的參數匹配器。
我們還是用例子來說話:
要測試的接口
package MockTest;public interface Service {    void execute(Request request, MData[] mdata, int mode);}參數類型定義

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 Code
package 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自定義匹配器
假設在我們的這個單獨的測試案例中,我們有以下參數匹配邏輯: 如果condition為true,則只需要比較value1;如果condition為false,則只需要比較value2. 由于這個邏輯和默認的equals方法不一致,因此我們不能直接使用equals方法,只能實現自己的參數匹配器。
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;    }}EqualsMData是為了演示當參數是對象數組的時候怎么實現參數匹配的.關鍵是要把Object對象強制性轉換為對象數組.
package MockTest;import org.easymock.EasyMock;import org.easymock.IArgumentMatcher;//實現IArgumentMatcher接口class EqualsMData implements IArgumentMatcher {    private MData[] expect;    private MData[] actual;    public EqualsMData(MData[] expect) {        this.expect = expect;    }    public static MData[] ZSMDataEquals(MData[] expect) {        //提交匹配要的自定義類        EasyMock.reportMatcher(new EqualsMData(expect));        return null;    }        @Override    //這個方法實現匹配參數的邏輯    public boolean matches(Object argument) {    //this method only can mathch one single parameter        System.out.println("argument is" + argument);        // TODO Auto-generated method stub        if (argument == this.expect)            return true;        if (!(argument instanceof MData[]))            return false;                //matches沒有提供接收數組的方法, 所以這里必須強制轉換OjectweiMData[]        actual = (MData[]) argument;        int length = expect.length;        if (length != actual.length)            return false;        for (int i = 0; i < length; i++) {            // if (expect[i].key != actual[j].key || expect[i].data != actual[j].data) //error            if (!expect[i].toString().equals(actual[i].toString()))            // if(!Arrays.equals(expect, actual))//error            {                return false;            }        }        return true;    }    @Override    //這個方法是匹配錯誤后要打印的信息    public void appendTo(StringBuffer buffer) {        // TODO Auto-generated method stub        buffer.append("EqualsMPut expect is: /n");        for (int i = 0; i < expect.length; i++) {            buffer.append(expect[i].toString());        }        buffer.append(" but actual is: /n");        for (int j = 0; j < actual.length; j++) {            buffer.append(expect[j].toString());        }    }}測試
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");//參數匹配器每次只能實現一個參數匹配,所以對于多個參數,要實現多個自定義匹配器        service.execute(                RequestMatcher.requestEquals(expectedCondition, expectedValue),                EqualsMData.ZSMDataEquals(datas), anyInt());        EasyMock.expectLastCall();        EasyMock.replay(service);//        MData[] datas2 = { new MData("1001".getBytes(), "2001".getBytes())};        service.execute(request, datas, 1);        EasyMock.verify(service);    }}新聞熱點
疑難解答