简单的PowerMock测试给出了一个错误,android studio项目

时间:2016-12-22 07:41:07

标签: java android unit-testing mockito powermock

我在Android Studio中有一个项目。我想用Mockito和PowerMock框架测试一些功能。 Powermock在简单测试中给出初始化错误,如下所示:

java.lang.NoClassDefFoundError: org/powermock/reflect/proxyframework/ClassLoaderRegisterProxyFramework

    at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.registerProxyframework(AbstractCommonTestSuiteChunkerImpl.java:101)
    at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.chunkClass(AbstractCommonTestSuiteChunkerImpl.java:114)
    at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.<init>(AbstractCommonTestSuiteChunkerImpl.java:60)
    at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.<init>(AbstractCommonTestSuiteChunkerImpl.java:54)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:58)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:98)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.powermock.reflect.proxyframework.ClassLoaderRegisterProxyFramework
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

我收到错误:

testCompile files('libs/mockito-all-2.0.2-beta.jar')
testCompile files('libs/mockito-core-2.0.5-beta.jar')
testCompile files('libs/powermock-api-mockito-1.6.5.jar')
testCompile files('libs/powermock-api-mockito-common-1.6.5.jar')
testCompile files('libs/powermock-core-1.6.5.jar')
testCompile files('libs/powermock-module-junit4-1.6.5.jar')
testCompile files('libs/powermock-module-junit4-common-1.6.5.jar')
testCompile files('libs/powermock-api-support-1.6.5.jar')
testCompile files('libs/javassist-3.14.0-ga-1.0.0.jar')
testCompile files('libs/cglib-2.2.2.jar')

我已检查过我的依赖项版本:

isSidebarOpen

Mockito运作良好,但Powermock仍然出错。

请帮助

1 个答案:

答案 0 :(得分:1)

我添加了一个依赖项:testCompile files('libs/powermock-reflect-1.6.5.jar')

我还将javassist的版本更改为:testCompile files('libs/javassist-3.18.2-GA.jar')

它解决了我的问题。