我在项目中成功地使用了Mockito,然后我决定添加一些Espresso测试(如下所述:https://developer.android.com/training/testing/ui-testing/espresso-testing.html#build)我被卡住了——当我在我的build.gradle中添加意式浓缩咖啡时,我会遇到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.sespresso:sespresso-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.spy
或Mockito.mock
方法上失败。类似于:
CustomClass1 li1 = Mockito.spy(new CustomClass1(somevar));
或
View vm= Mockito.mock(View.class);
任何帮助都将不胜感激!
这不是Mockito
的问题,而是Powermock
的问题,它依赖于旧版本的Mockito
。
更新此库依赖项或从Powermock
渐变依赖项中排除mockito
。