我在模拟仪器(Espresso
)测试的静态方法上很难。对于模拟对象,我正在使用Mockito
。但是,由于Mockito
无法模拟静态方法,因此我在其顶部使用Powermock
。这对于在JVM机上运行的测试效果很好,但是对于UI测试,此组合不正常。我宣布了以下仪器测试的依赖项。
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'org.powermock:powermock-api-mockito:1.6.5'
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.5'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
我正在编写下面的样本测试。
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(AndroidJUnit4.class)
public class SampleTest {
@Test
public void test(){
//Test Code.
}
}
代码成功编译,但是在运行时,它给了我以下异常
java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath.
at org.powermock.reflect.proxyframework.ProxyFrameworkHelper.register(ProxyFrameworkHelper.java:35)
at org.powermock.reflect.proxyframework.ClassLoaderRegisterProxyFramework.registerProxyframework(ClassLoaderRegisterProxyFramework.java:28)
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 java.lang.reflect.Constructor.newInstance(Native Method)
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:1879)
我还想念什么?有什么方法可以在浓缩咖啡测试中模拟静态方法?
powermock与浓缩咖啡不起作用,请参阅此问题
您可以使用PowerMock来模拟静态方法。我用它在我的应用程序中模拟了log.java类。在下面的示例上查看:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Log.class})
public class PowermockStaticExample {
//fixme : complete with your members
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(YourStaticClass.class);
//Use Powermock annotation to mock what you want to
}
//fixme : complete with your methods to test