>我正在尝试在我的安卓项目上设置 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)
最后我发现了问题,您正在尝试在Dalvik VM(Android仪器测试)上运行PowerMockito测试。这是不可能的,你应该使用 JVM 来运行 PowerMockito 测试(Android Studio 单元测试)。
随着Android Studio的1.1版本,引入了对可以在JVM上运行的单元测试的支持。
因此,要解决此问题,您需要按照本教程 (https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support) 使单元测试能够在 JVM 上运行。