Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

mocking - How to use PowerMock in Android projects?

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 to the test project's libs folder. The test class is a very simple object:

package com.test.test;

import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

import android.util.Log;

@RunWith(PowerMockRunner.class)
public class TestTestAndroid {

    public void testRuns() {
        Log.e("test", "Test case is called");
    }
}

Then, I try running the project from Eclipse, or making the project from the command line. I get the same error:

Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/hamcrest/Description;

As it turns out, both junit-4.8.2.jar and mockito-all-1.9.5.jar define org.hamcrest.Description. I must include the Mockito jar for obvious reasons - I need Mockito. A different version of JUnit is provided by Android, but it is an old version that does not include the @RunWith annotation.

Can someone answer how to use powermock and mockito in an Android project, without the conflicting org.hamcrest.Description problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...