How to use PowerMock in Android projects?

前端 未结 2 1187
野性不改
野性不改 2020-12-31 05:54

I created a new Android test project. I downloaded powermock-mockito-junit-1-1.5.zip from https://code.google.com/p/powermock/downloads/list. I added all of the libraries

相关标签:
2条回答
  • 2020-12-31 06:04

    Actually, I find that this is completely untrue (at least with the latest version).

    In my gradle setup for the module where I want to test my code I have

    // Mockito and PowerMock
    androidTestCompile ('org.powermock:powermock-mockito-release-full:1.6.0') {
        exclude module: 'hamcrest-core'
        exclude module: 'objenesis'
    }
    

    and everything runs fine. As long as I use:

    @RunWith(PowerMockRunner.class) @PrepareForTest(ClassToMock.class)
    

    to annotate my class (eg, can't run it with Robolectric, still fine in CI environments)

    0 讨论(0)
  • 2020-12-31 06:11

    Sorry, you can't use PowerMock in the Dalvik VM.

    PowerMock works by running your test under a custom ClassLoader which uses Javassist to modify the bytecode of your classes. This works okay on a normal JVM, but on Dalvik the bytecode and class format are different, so this approach doesn't work. PowerMock would need to be rewritten to use Dexmaker instead of Javassist - this would be decidedly non-trivial, and I don't see anything like this on the PowerMock issues list.

    0 讨论(0)
提交回复
热议问题