Espresso添加到项目时Mockito异常

时间:2016-08-28 20:23:37

标签: android testing mockito android-espresso

我在我的项目中成功使用了Mockito,然后我决定添加一些Espresso测试(如此处所述:https://developer.android.com/training/testing/ui-testing/espresso-testing.html#build)并且我被卡住 - 当我向我的build.gradle添加espresso时我遇到了问题的Mockito。

当Mockito正常工作时,我在build.gradle中的依赖关系,例如我的测试使用Mockito传递:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    testCompile 'junit:junit:4.12'
    //Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
    // https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4
    testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.6.4'
    testCompile 'org.powermock:powermock-api-mockito:1.5.5'
    androidTestCompile ('com.android.support:support-annotations:23.4.0')
    //androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2')
}

当我取消注释最后一行以包含com.android.support.test.espresso:espresso-core:2.2.2并且我再次运行我的测试时,我得到以下错误:

java.lang.IllegalAccessError: class org.mockito.internal.creation.jmock.ClassImposterizer$1 cannot access its superclass org.mockito.internal.creation.cglib.MockitoNamingPolicy

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.mockito.internal.creation.jmock.ClassImposterizer.<clinit>(ClassImposterizer.java:38)
    at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
    at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:43)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.spy(Mockito.java:1368)
    at todolistj.todolist.unittest.CustomClass.size(CustomClass.java:54)

所有失败的测试都在Mockito.spyMockito.mock方法失败。类似的东西:

CustomClass1 li1 = Mockito.spy(new CustomClass1(somevar));

View vm= Mockito.mock(View.class);

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

Mockito没有问题,Powermock没有问题,这取决于Mockito的旧版本。

更新此库依赖项或从mockito gradle依赖项中排除Powermock