Mockito spy does not work

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I am trying to test my android project by Mockito.

But it seems that spy does not work as I think. Isn't a spy object is equal to a real object?

when I try to call a method of spy object, testing result alway is AbstractMethodError(just as follows).

java.lang.AbstractMethodError: abstract method not implemented at com.google.dexmaker.mockito.InvocationHandlerAdapter$ProxiedMethod.isAbstract(InvocationHandlerAdapter.java) at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:109) at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41) at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93) at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29) at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38) at com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:49) at ArrayList_Proxy.add(ArrayList_Proxy.generated) 

My test case:

public void testGetAllCountryKeywords_WithSpy(){     List<String> list = new ArrayList<>();     List spy = spy(list);     spy.add("hi");  } 

Here is the dependencies in my build.gradle.

dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     androidTestCompile 'junit:junit:4.12'     androidTestCompile "com.google.dexmaker:dexmaker:1.+"     androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.+"     androidTestCompile 'org.mockito:mockito-core:1.10.+'     androidTestCompile 'com.android.support.test:runner:0.3' } 

Can someone give me some suggestion? I really have no idea. Thanks!

回答1:

You are missing the generic type, reference 'spy' proxy can not know its type

public void testGetAllCountryKeywords_WithSpy(){ List list = new ArrayList<>(); List spy = spy(list); spy.add("hi");

}



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!