2015-07-10 60 views
3

我想通過Mockito測試我的Android項目。Mockito間諜不工作

但似乎間諜並不按我的想法工作。 間諜對象是不是等於一個真實的對象?

當我嘗試調用間諜對象的方法時,測試結果一直是AbstractMethodError(如下)。

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的依賴關係。

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' 
} 

有人可以給我一些建議嗎?我真的不知道。謝謝!

+5

可能1.10的Mockito以及dexmaker 1.2之間的一個錯誤。你可以嘗試強制mockito 1.9.5,看看是否修復它? https://code.google.com/p/dexmaker/issues/detail?id=43 –

+0

同樣的問題在這裏。我正在使用2.0.7-beta。使用版本1.9.5對其進行排序。 – Darren

+1

@SkinnyJ將版本更改爲1.9.5後,間諜工作!謝謝! –

回答

0

你缺少泛型類型,參考 '間諜' 的代理無法知道它的類型

公共無效testGetAllCountryKeywords_WithSpy(){ 名單列表=新的ArrayList <>(); List spy = spy(list); spy.add(「hi」);

}

+0

對不起。看來這不是造成這個錯誤的原因。我嘗試添加泛型類型,但間諜仍然不起作用。 –

+0

@TobeyLin你得到的錯誤究竟是什麼 – kuhajeyan

+0

正如「抽象方法未實現」一樣, –