今天在项目中采用powermock进行单元测试,发现使用Matchers结果报如下错误
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
4 matchers expected, 3 recorded:
-> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java:136)
-> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java:136)
-> at com.yihaodian.wap.service.AddressServiceTest.testInsertGoodReceiverByTokenV2(AddressServiceTest.java:136)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
原始代码如下:
httpHelpMock.callShopInterfaceObj("addGoodReceiver", Matchers.any(Object[].class), Matchers.any(Type.class),"/myyhdmobile/address/addGoodReceiver.do",true);
public void callShopInterfaceObj(String methodName, Object[] objs,Type t,String shopurl2,Object reObj){
PowerMockito.when(HttpHelper.callShopInterfaceObj(methodName, objs, t, shopurl2)).thenReturn(reObj);
}
怎么也调试不出来
最后查看了这篇文章
http://stackoverflow.com/questions/14845690/mockito-invaliduseofmatchersexception
做修改如下:
httpHelpMock.callShopInterfaceObj(Matchers.eq("addGoodReceiver"), Matchers.any(Object[].class), Matchers.any(Type.class),Matchers.eq("/myyhdmobile/address/addGoodReceiver.do"),true);
主要差异是对传入的字符串做了Matchers.eq()动作
原因:使用Matchers不能只针对部分参数,所有参数都应该采用Matchers