Re: [powermock] Using PowerMock with Robolectric runner

2,298 views
Skip to first unread message

Johan Haleby

unread,
Mar 15, 2013, 2:40:30 AM3/15/13
to powe...@googlegroups.com
Yes using a Rule is probably the only option that may work. Please use the java agent based implementation though since it seems to be more stable.

What is it you want to achieve when doing: 

GameCatalogContentProvider provider = new GameCatalogContentProvider();

Regards,
/Johan


On Tue, Mar 12, 2013 at 3:26 PM, Lior Tal <lior...@gmail.com> wrote:
Hello,

I am using Robolectric to mock and test Android apps, and i'd like to use it in conjunction with PowerMock to for mocking constructor calls (whenNew).

Last word i've seen on this matter was that since both frameworks use a test runner, newer versions of PowerMock enable the use of a PowerMockRule to allow using a different runner while using PowerMock's loader.

The code i'm using now:

@RunWith(RobolectricTestRunner.class)
public class Tests {

    @Rule
    public PowerMockRule powerMockRule = new PowerMockRule();

    // this is a simple Android ContentProvider derived class. nothing special here.
    GameCatalogContentProvider provider = new GameCatalogContentProvider();
}

This throws an exception when running the test (seems to not have used Robolectric's mocking).

If i remove the @Rule annotated line, this works fine.

What am i doing wrong or missing?

java.lang.ExceptionInInitializerError
at GameCatalogContentProviderTest.useAppsTable(GameCatalogContentProviderTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: Stub!
at android.net.Uri.parse(Uri.java:53)
at com.example.gamecat.GameCatalogContentProvider.<clinit>(GameCatalogContentProvider.java:19)
... 37 more

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Johan Haleby

unread,
May 16, 2013, 2:29:04 AM5/16/13
to powe...@googlegroups.com
Have you tried to use it in combination with the PowerMock Junit rule? If that doesn't work I don't really know. I tried to get it working a couple of years ago but I failed because of classloading conflicts/race-conditions.

/Johan


On Thu, May 16, 2013 at 2:11 AM, Matthew Boetger <mboe...@pandora.com> wrote:
Has there been any successful integrations of Robolectric and PowerMock?  I have been trying to combine the two, but without any luck.  

Matthew Boetger

unread,
May 16, 2013, 12:55:42 PM5/16/13
to powe...@googlegroups.com
I have.  It seems like the Powermock Rule overrides the Robolectric Class Loader and causes Stub Exceptions to be thrown from the Android SDK.  I've started going through the code to see if I can find an easy fix.  It seems like Powermock should be able to grab the current classloader instead of the system class loader.

Johan Haleby

unread,
May 17, 2013, 12:59:54 AM5/17/13
to powe...@googlegroups.com
If you manage to find anything please let us know! It would be really cool to get it working.

Just to be clear, have you tried the java agent based PowerMock rule (there are two rules, one classloader based and one java agent based)? 

Regards,
/Johan

Leonard Ng'eno

unread,
Dec 9, 2013, 12:32:01 PM12/9/13
to powe...@googlegroups.com
Has anyone managed to successfully run tests using Robolectric and PowerMock? Would really appreciate it.

Johan Haleby

unread,
Dec 9, 2013, 1:32:18 PM12/9/13
to powe...@googlegroups.com
You could try the java agent, not sure if it'll work but it's worth a shot. Please tell us if it works or not!

/Johan 


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.

Vincent Huang

unread,
Jan 16, 2015, 3:57:06 AM1/16/15
to powe...@googlegroups.com
It works to me via below method

app/build.gradle
dependencies {
    androidTestCompile 'junit:junit:4.10'
    androidTestCompile 'org.robolectric:robolectric:2.1.+'
    androidTestCompile 'com.squareup:fest-android:1.0.+'
    androidTestCompile 'org.mockito:mockito-all:1.9.5'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.1'
    androidTestCompile 'org.powermock:powermock-api-mockito:1.5.4'
    androidTestCompile 'org.powermock:powermock-module-junit4:1.5.4'
    androidTestCompile 'org.powermock:powermock-core:1.5.4'
    androidTestCompile 'org.powermock:powermock-module-junit4-rule:1.5.4'
    androidTestCompile 'org.powermock:powermock-classloading-base:1.5.4'
    androidTestCompile 'org.powermock:powermock-classloading-xstream:1.5.4'
    androidTestCompile 'org.powermock:powermock-module-testng:1.5.4'
}


@RunWith(RobolectricGradleTestRunner.class)
@PrepareForTest({StaticClass.class})
@PowerMockIgnore({"org.robolectric.*", "android.*"})
public class SampleTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Test
    public void testSample() throws Exception {
        
PowerMockito.when(StaticClass.isPassed()).thenReturn(true);

        Assert.assertEquals(StaticClass.isPassed(), true);
    }
}

在 2013年3月12日星期二 UTC+8下午10:26:25,Lior Tal写道:

Johan Haleby

unread,
Jan 16, 2015, 11:31:31 AM1/16/15
to powe...@googlegroups.com
You can also try to use the new DelegatingRunner.

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nikki Shah

unread,
Apr 13, 2015, 4:02:43 PM4/13/15
to powe...@googlegroups.com
Hey Johan, 

Were you able to get it to run with the Delegate Runner?

Cheers, 

Nikki

Johan Haleby

unread,
Apr 15, 2015, 5:33:52 AM4/15/15
to powe...@googlegroups.com
I've never tried it myself. I would be interested to know if people do get it working with the delegating runner though.

Anushree Ganjam

unread,
Sep 30, 2016, 4:07:25 PM9/30/16
to PowerMock
Hi, 
Were you able to solve this problem ?
I am using  @RunWith(RobolectricTestRunner.class)
and  @Rule
 public PowerMockRule rule = new PowerMockRule();

I get java.lang.ExceptionInInitializerError 

Caused by: java.lang.IllegalStateException: PowerMockRule can only be used with the system classloader but was loaded by org.robolectric.internal.bytecode.InstrumentingClassLoader@6ebc05a6
at org.powermock.modules.junit4.rule.PowerMockRule.<clinit>(PowerMockRule.java:35)
... 27 more

I want to mock static methods using PowerMock and run using RobolectricTestRunner.

Please let me know how to solve this.

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages