gpt4 book ai didi

android - 在 Android 上使用 PowerMockito 模拟静态方法

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:29 25 4
gpt4 key购买 nike

我正在尝试 stub 静态方法,以便我可以对 REST Api 接口(interface)进行单元测试。

我正在使用...

  • PowerMockito 1.5(启用静态模拟)
  • Roboelectric 1.2(淘汰 android 的其余部分)
  • JUnit 4.10

运行以下代码会出现后续错误

有什么办法可以解决这个问题吗?

    @RunWith(RobolectricTestRunner.class)
@PrepareForTest({Api.class})
public class ApiTest extends TestCase {

@Rule
public PowerMockRule rule = new PowerMockRule();
...
@Test
public void testGet() throws Exception {
Api.Response fakeResponse = PowerMockito.mock(Api.Response.class);
PowerMockito.when(fakeResponse.getResult()).thenReturn(responseObj);
mockStatic(Api.class);
PowerMockito.when(Api.execute(any(HttpRequestBase.class))).thenReturn(fakeResponse);
Api.get("/v1/contacts/");
}
}

然而,在我们调用“Api.get”之前静态上的 stub 抛出以下错误...

java.lang.RuntimeException: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for org.powermock.classloading.DeepCloner
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:68)
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:34)
at org.powermock.classloading.ClassloaderExecutor.createDeepCloner(ClassloaderExecutor.java:106)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:88)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for org.powermock.classloading.DeepCloner
at javassist.Loader.findClass(Loader.java:360)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(RobolectricClassLoader.java:83)
at javassist.Loader.loadClass(Loader.java:312)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:59)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66)
... 24 more
Caused by: com.xtremelabs.robolectric.bytecode.IgnorableClassNotFoundException: msg because of javassist.NotFoundException: org.powermock.classloading.DeepCloner
at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:92)
at javassist.Loader.findClass(Loader.java:341)
... 31 more

最佳答案

要进行静态模拟,您必须使用 PowerMockRunner,否则它不起作用。请参阅文档。

如果可能的话,由于类加载的复杂性,我总是避免静态模拟。如果您愿意分享经过测试的代码,也许我可以建议一些解决方法。通常我在类中创建一个非静态方法,它使用静态方法,我在这个非静态方法中调用静态方法。然后我只是对被测试的类进行监视,我只模拟这个方法。这样我就不需要 PowerMock。

关于android - 在 Android 上使用 PowerMockito 模拟静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com