使用PowerMock时的ClassNotFoundException - Android Junit

时间:2017-08-15 17:03:29

标签: java android junit powermock powermockito

我正在尝试编写一些使用PowerMock / PowerMockito的测试,但出于某种原因,当我尝试在该类中运行测试时,我一直在Test类上获得ClassNotFoundException。这是我的app.gradle powerMockito声明:

androidTestCompile 'org.powermock:powermock:1.6.6'
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6'
androidTestCompile 'org.powermock:powermock-api-mockito:1.6.6'

这是我测试课的开始:

@RunWith(PowerMockRunner.class)
@PrepareForTest(SettingsView.class)
public class SettingsTests {

  private SettingsView view;

...

这是我收到的堆栈跟踪:

java.lang.ClassNotFoundException: com.app.SettingsTests
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:400)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:161)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:48)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:111)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:71)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:77)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:789)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:753)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.IllegalStateException: Failed to transform class with name com.app.SettingsTests. Reason: com.app.SettingsTests
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:284)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:192)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:77)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 27 more
Caused by: javassist.NotFoundException: com.app.SettingsTests
at javassist.ClassPool.get(ClassPool.java:452)
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:262)
... 31 more

有谁知道可能导致这种情况的原因是什么? Junit版本是4.12

1 个答案:

答案 0 :(得分:0)

我不确定这是否适用于Android。我在我的网络项目上有类似的问题,并且压倒javaassist依赖对我有用。试一试。

testCompile group: 'org.javassist', name: 'javassist', version: '3.18.2-GA'