Testing 莫基托间谍行不通

Testing 莫基托间谍行不通,testing,mockito,Testing,Mockito,我正试图通过Mockito测试我的android项目 但那个间谍似乎并不像我想的那个样有效。 间谍对象不等于真实对象吗 当我尝试调用spy对象的方法时,测试结果总是AbstractMethodError(如下所示) 我的测试用例: public void testGetAllCountryKeywords_WithSpy(){ List<String> list = new ArrayList<>(); List spy = spy(list);

我正试图通过Mockito测试我的android项目

但那个间谍似乎并不像我想的那个样有效。 间谍对象不等于真实对象吗

当我尝试调用spy对象的方法时,测试结果总是AbstractMethodError(如下所示)

我的测试用例:

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

}

有人能给我一些建议吗?我真的不知道。谢谢

您缺少泛型类型,引用“spy”代理无法知道其类型

public void testGetAllCountryKeywords_with Spy(){ 列表=新的ArrayList(); 列表间谍=间谍(列表); spy.add(“hi”)

}


可能是mockito 1.10和dexmaker 1.2之间的错误。你能试着强制mockito 1.9.5,看看是否能解决它吗?这里也有同样的问题。我用的是2.0.7-beta版。使用1.9.5版对其进行排序。@SkinnyJ在我将版本更改为1.9.5后,spy工作了!谢谢@你救了我一天!很抱歉看来这并不是导致这一错误的原因。我试图添加泛型类型,但spy仍然不起作用。@TobeyLin您得到的错误与“abstract method not implemented”相同
public void testGetAllCountryKeywords_WithSpy(){
    List<String> list = new ArrayList<>();
    List spy = spy(list);
    spy.add("hi");

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