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?
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.