I am trying to use PowerMock in my unit test (JUnit 4.12).
I already have Mockito without any problems. This is an Android app.
When I run my unit test, there is no problem, and mocking a static function works fine.
When I click the play button in eclipse to run my application on a connected physical machine, I get this error:
[2015-01-15 15:22:22 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/hamcrest/Description; [2015-01-15 15:22:22 - CLAP] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/hamcrest/Description;
I read that this means that PowerMock does not support the Delvik virtual machine, but I do not understand what this means, and I cannot believe that the PowerMock team or the Mockito team did not find a way to work in the Android environment!
Here are my Maven dependencies related to PowerMock and Mockito
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.1</version> <scope>test</scope> </dependency>
Can someone help me?
EDIT
I am sure that all I need to do is remove PowerMock from my dependencies when I actually run my application (not in test), but I don’t know how to do it. I use Eclipse, so I need a solution that will work in this environment. I checked the profile and the exception from Maven, but I do not understand how to do this. I am very new to maven. Any help would be greatly appreciated.
Also, when I remove PowerMock dependencies (and all unit test using it), the project can now work on my device without problems.
EDIT 2
Using the mvn dependency:list command suggested in the comment, I found the following:
- JUnit has a dependency on org.hamcrest: hamcrest-core: jar: 1.3: test
- PowerMock also has a dependency on the hamcrest library
It seems that the problem only occurs when it is the version of the library that is used in the project. I tried to remove the JUnit dependency and use only powermock, and the startup error is the same. So I don’t think this is a “collision” problem, but maybe the problem is with the version of hamcrest that comes with powermock ??? And I wonder why it is used at startup, as it is in the “check” area ...
EDIT 3 I created an Android project from scratch with Maven to find out if there is a problem with my main application or with Maven. The problem seems to be in Maven OR depending on PowerMock. If you want to try, here is a complete java project . There is no unit test in this project, I just want to run it on my Android machine. I still get the same message.