尝试使用 PowerMockito 存根 Android Activity 类会抛出 RuntimeException“Stub!”
我发现这个示例,他们使用 PowerMock 和 EasyMock 来存根/模拟 Menu 和 MenuItem 类安卓。我一直在尝试使用 PowerMock 和 Mockito 的 Activity 类做类似的事情。
我知道很多方法都是最终方法,并且在 Android.jar 中它们都只是抛出 RuntimeException("Stub!")。
我也知道这个测试尚未完成,但我只是想看看是否可以模拟 android Activity 类。
但是考虑到 PowerMock 允许您使用最终方法模拟类,这段代码不应该工作吗?
@RunWith(PowerMockRunner.class)
@PrepareForTest(Activity.class)
public class MyTestCase extends TestCase {
public void testPlease_JustWork() throws Exception {
Activity mockActivity = PowerMockito.mock(Activity.class);
PowerMockito.when(mockActivity.getTitle()).thenReturn("Title");
}
}
我认为 RuntimeException 将不再发生并且“Title”将被返回,但它仍然抛出异常。
我尝试过各种不同的方法,例如 doReturn("Title").when(mockActivity).getTitle();
和 suppress(constructor(Activity.class));
我做错了什么还是这根本不可能?
I found this example where they used PowerMock and EasyMock to stub/mock the Menu and MenuItem classes for android. I have been trying to do something similar with PowerMock and Mockito with the Activity class.
I understand that a lot of the methods are final and that in the Android.jar they all just throw RuntimeException("Stub!").
I also understand that this test isn't complete but I just wanting to see if it is possible to mock the android Activity class.
But given that PowerMock allows you to mock classes with final methods shouldn't this code work?
@RunWith(PowerMockRunner.class)
@PrepareForTest(Activity.class)
public class MyTestCase extends TestCase {
public void testPlease_JustWork() throws Exception {
Activity mockActivity = PowerMockito.mock(Activity.class);
PowerMockito.when(mockActivity.getTitle()).thenReturn("Title");
}
}
I would think that the RuntimeException would no longer occur and "Title" would be returned but it still throws the exception.
I have tried all sorts of different things like doReturn("Title").when(mockActivity).getTitle();
and suppress(constructor(Activity.class));
Am I doing something wrong or is this just not possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能不是您问题的直接答案,但您可以尝试 Robolectric 来测试您的部分内容您的 PC 上的 Android 应用程序。
Roboelectric 避开了 Stub!异常并为您提供一些最小 Android 类实现
Probably not a direct answer to your question, but you could try Robolectric in order to test parts of your Android app on your PC.
Roboelectric avoids the Stub! exception and gives you some minimal Android classes implementation
我认为答案是引用的库的顺序很重要,因为 android.jar 包含一些 junit 的存根。
您必须确保在您的测试项目中,如果您转到“属性”,然后转到“Java 构建路径”,则与您下载的完整版 powermock 关联的 junit jar 显示在“订购和导出”选项卡上 位于 android.jar 之上。如果没有,系统会先从 android.jar 解析 junit.framework 和 junit.runner 包,然后再从 powermock 中包含的 junit.jar 解析它们。
我意识到这个问题已经很老了,但我认为这是正确的答案,所以我想确保在这个问题上记录下来。
I think the answer is that the order of your referenced libraries matters because the android.jar includes some stubs for junit.
You have to ensure that on your test project, if you go to 'properties' and then 'Java Build Path' that the junit jar associated with the full version of powermock you downloaded show up on the 'Order and Export' tab above the android.jar. If it does not, the system resolves the junit.framework and junit.runner packages from android.jar before it resolves them from the junit.jar included in powermock.
I realize this question is old, but I think this is the proper answer so i wanted to make sure to document it on the question.
我刚刚尝试了你的代码示例,它在这里工作,很奇怪。我下载了包含依赖项的 PowerMock 1.4.5 以及 Mockito 和 JUnit,并使用了 sdk (2.2) 中的 android.jar。如果我删除@PrepareForTest,它只会失败。
编辑
您可以使用 android.jar 与您引用的文章中提供的已删除的异常代码。
I just tried your code sample and it works here, strange. I downloaded PowerMock 1.4.5 with Mockito and JUnit including dependencies and used the android.jar from the sdk (2.2). It only fails with the exception if i remove the @PrepareForTest.
EDIT
You could use the android.jar with the removed exception code, provided in the article you referenced.