Mockito 间谍不起作用 [英] Mockito spy does not work
问题描述
我正在尝试通过 Mockito 测试我的 android 项目.
I am trying to test my android project by Mockito.
但似乎 spy 并不像我想的那样工作.间谍对象不等于真实对象吗?
But it seems that spy does not work as I think. Isn't a spy object is equal to a real object?
当我尝试调用spy对象的方法时,测试结果总是AbstractMethodError(如下).
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)
我的测试用例:
public void testGetAllCountryKeywords_WithSpy(){
List<String> list = new ArrayList<>();
List spy = spy(list);
spy.add("hi");
}
这是我的 build.gradle 中的依赖项.
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!
推荐答案
您缺少泛型类型,引用 'spy' 代理无法知道其类型
You are missing the generic type, reference 'spy' proxy can not know its type
public void testGetAllCountryKeywords_WithSpy(){List list = new ArrayList<>();列表间谍 = 间谍(列表);spy.add("hi");
public void testGetAllCountryKeywords_WithSpy(){ List list = new ArrayList<>(); List spy = spy(list); spy.add("hi");
}
这篇关于Mockito 间谍不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!