Mockito spy not working

I am trying to test my Android project from Mockito.

But it seems that the spy is not working as I think. Isn't the spy object equal to the real object?

when I try to call a method of the spy object, the validation result is always AbstractMethodError (as shown below).

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 are 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. Thank!

+3


source to share


1 answer


You don't need a generic type, the "spy" link proxy cannot know its type



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

}

0


source







All Articles