我正在尝试在我的android项目上设置power mokito,以测试静态方法
我写了这些依赖项
provided 'junit:junit:4.11'
androidTestCompile ('junit:junit:4.11'){
exclude module:"hamcrest-core"
exclude module: "assertj-core"
}
// provided 'org.mockito:mockito-core:2.0.3-beta'
// androidTestCompile ('org.mockito:mockito-core:2.0.3-beta'){
// exclude module:"hamcrest-core"
// exclude module:"assertj-core"
// }
provided 'org.powermock:powermock-mockito-release-full:1.4.9'
androidTestCompile ('org.powermock:powermock-mockito-release-full:1.4.9'){
exclude module : "hamcrest-core"
// exclude module : "mockito-all"
exclude module: "assertj-core"
exclude module: "objenesis"
}
然后我写了我的测试
import android.test.AndroidTestCase;
import com.myapp.IntentActions;
import org.junit.Test;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
public class IntentActionsTest extends AndroidTestCase {
@Test
public void testIntentFilter() {
//IntentActions class is a factory
//for intent filters
//I want to make sure that the filter is set up correctly
//This is why I want to use PowerMockito
mockStatic(IntentActions.class);
}
}
问题是当我运行此测试时出现此错误
java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:68)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:50)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:100)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
at com.myapp.tests.IntentActionsTest.testIntentFilter(IntentActionsTest.java:22)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
答案 0 :(得分:1)
最后我发现了问题,您正在尝试在Dalvik VM(Android Instrumentation Test)上运行PowerMockito测试。您无法使用JVM来运行PowerMockito测试(Android Studio单元测试)。
随着Android Studio 1.1版的引入,对可以在JVM上运行的单元测试提供了支持。
因此,要解决此问题,您需要遵循本教程(https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support)以使单元测试能够在JVM上运行。