4

I'm trying to stub out a static method so that I can unit test a REST Api interface.

I'm using...

  • PowerMockito 1.5 (to enable mocking of statics)
  • Roboelectric 1.2 (to stub out the rest of the android)
  • JUnit 4.10

Running the following code gives me the subsequent error

Any idea on what could fix this?

    @RunWith(RobolectricTestRunner.class)
    @PrepareForTest({Api.class})
    public class ApiTest extends TestCase {

        @Rule
        public PowerMockRule rule = new PowerMockRule();
        ...
        @Test
        public void testGet() throws Exception {
            Api.Response fakeResponse = PowerMockito.mock(Api.Response.class);
            PowerMockito.when(fakeResponse.getResult()).thenReturn(responseObj);
            mockStatic(Api.class);
            PowerMockito.when(Api.execute(any(HttpRequestBase.class))).thenReturn(fakeResponse);
            Api.get("/v1/contacts/");
        }
    }

However the stub on the static right before we call "Api.get" throws the following error...

java.lang.RuntimeException: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for org.powermock.classloading.DeepCloner
    at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:68)
    at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:34)
    at org.powermock.classloading.ClassloaderExecutor.createDeepCloner(ClassloaderExecutor.java:106)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:88)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
    at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for org.powermock.classloading.DeepCloner
    at javassist.Loader.findClass(Loader.java:360)
    at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(RobolectricClassLoader.java:83)
    at javassist.Loader.loadClass(Loader.java:312)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:59)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66)
    ... 24 more
Caused by: com.xtremelabs.robolectric.bytecode.IgnorableClassNotFoundException: msg because of javassist.NotFoundException: org.powermock.classloading.DeepCloner
    at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:92)
    at javassist.Loader.findClass(Loader.java:341)
    ... 31 more
Ryan Pfeffer
  • 865
  • 10
  • 19

1 Answers1

1

To have static mocking you have to use the PowerMockRunner, otherwise it does not work. See the documentation.

If possible i always avoid static mocking because of the classloading complications. If you would share the tested code, maybe I could suggest some workaround. Usually i create a non static method inside the class, which uses the static method, and i call the static method inside this non static one. Then i simply make a spy of the tested class, and i mock only this method. This way i do not need PowerMock.

Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
  • I just ran into this problem and this was the only reference to this bug I could eventually find - I kept thinking I must be doing something wrong. Where in the docs is this stated? I can confirm this is the case - after reducing my test to the bare essentials, static mocking stops working when switching from the runner to the rule. This is too bad, since it means you lose a lot of PowerMock's value when you have to run under a different runner. – Tom Tresansky Aug 21 '14 at 14:37
  • @Tom Tresansky: you can give a try to the agent based powermocking, which lets you to use a different runner. See https://code.google.com/p/powermock/wiki/PowerMockAgent – Gábor Lipták Aug 21 '14 at 21:20
  • I thought this was what we were already trying to do? I've asked a new question at: http://stackoverflow.com/questions/25850673/is-it-possible-to-mock-a-static-method-on-a-final-class-using-a-powermockrule-in to fully clarify this. – Tom Tresansky Sep 15 '14 at 14:53