Android : PowerMock not stubbing static method properly?

on Wednesday, July 30, 2014


I'm trying to write a VERY simple test, using powermock and Robolectric. Here's my code:



import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.robolectric.RobolectricTestRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest(BadStaticClass.class)
public class SomeActivityTest {

@Test
public void testSomething() {
PowerMockito.mockStatic(BadStaticClass.class);
Mockito.when(BadStaticClass.dontWantThisMethod()).thenReturn(false);
new SomeActivity().usesStatic();
}
}


Basically, i have a class, "SomeActivity", and it has a method that makes a call to BadStaticClass.dontWantThisMethod(). i want that static method to be stubbed out.


why is my code not working?


i kept getting errors like: you are trying to stub a final method, you naughty developer!


which, i thought the whole point of PowerMock was to not see that.


0 comments:

Post a Comment