1

I need to mock a static method on an android instrumentation test. If i need to mock a static method, the test class needs to be @RunWith(PowerMockRunner.class) . But my instrumentation test are required to run with AndroidJUnit4.class .

Is it possible to have two runnable? or is there any other way i can use power mock to mock static methods? or any other options to mock static classes?

Libin
  • 16,967
  • 7
  • 61
  • 83

1 Answers1

4

To handle cases like this since version 1.4 it's possible to bootstrap PowerMock using a JUnit Rule instead of the runner. Something looking like this:

@RunWith(AndroidJUnit4.class)
@PrepareForTest(X.class);
public class MyTest {
    @Rule
    PowerMockRule rule = new PowerMockRule();

    // Tests goes here
    ...
}

But be aware that PowerMock is using byte-code manipulation which someone needs to convert to dalvikVM dex. And currently there are no tools to support that (https://groups.google.com/forum/#!topic/powermock/9kwPaWoZ_14 , https://stackoverflow.com/a/27956309/624706)

Community
  • 1
  • 1
Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71