问题描述
我发现这个例如的,他们使用PowerMock和EasyMock的存根/模拟菜单和菜单项类机器人。我一直在努力做的PowerMock和的Mockito与Activity类类似的东西。
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.
据我了解,有很多的方法是最终的,在他们的android.jar都只是抛出的RuntimeException(存根!)。
I understand that a lot of the methods are final and that in the Android.jar they all just throw RuntimeException("Stub!").
我也明白,这个测试是不完整的,但我只是想看看是否有可能嘲笑了Android Activity类。
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.
但考虑到PowerMock可以让你模拟教学班,最后的方法应该不是这个code的工作?
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");
}
}
我会认为RuntimeException的将不再发生,标题将返回,但它仍然抛出异常。
I would think that the RuntimeException would no longer occur and "Title" would be returned but it still throws the exception.
我已经尝试了各种不同的东西像 doReturn(标题)时(mockActivity).getTitle();
和燮preSS(构造函数(Activity.class));
I have tried all sorts of different things like doReturn("Title").when(mockActivity).getTitle();
and suppress(constructor(Activity.class));
难道我做错了什么,或者这是不可能的?
推荐答案
我只是想你code样品,它工作在这里,奇怪的。我下载PowerMock 1.4.5的Mockito和JUnit,包括依赖性和从SDK(2.2)使用的android.jar。它不但不能例外,如果我删除@ prepareForTest。
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.
修改
您可以使用的android.jar使用已删除的例外code,在你引用的文章中提供。
You could use the android.jar with the removed exception code, provided in the article you referenced.
这篇关于尝试使用PowerMockito抛出的RuntimeException&QUOT存根Android的Activity类;存根&QUOT!;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!