PowerMock + Mockito + Maven in an Android app displaying a Dex bootloader error

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.

+1
source share
1 answer

I finally fixed the problem, but I do not understand everything. Many thanks to Evgeny Martynov for help and for this post, which shows me the right direction.

The problem seems to be that Maven has a big problem with duplicate .jar files that are in nested dependencies. Using a class search, I found that the class name hamcrest "Description" was 3 times in my project!

So I did a little research on how to eliminate dependencies in Maven, and found that all of this can be done in the Maven Pom editor. You can click on the dependencies in the dependencies tab and do "Delete".

The rest of the problem was simply removing the duplicated Hamcrest dependencies so that there was only one dependency in my project. After fixing this problem in the second library, the same "obgenesis" problem arose. I made the same fix and it finally worked.

Evgeny Martynov pointed out to me in a comment that I did not use Maven at all. Maybe it's true, bear with me! At least now I know Maven a little more, and it does what needs to be done in my project.

Here is the latest dependency code:

 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <exclusions> <exclusion> <artifactId>hamcrest-core</artifactId> <groupId>org.hamcrest</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.10.8</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>objenesis</artifactId> <groupId>org.objenesis</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.1</version> <scope>test</scope> <optional>true</optional> <exclusions> <exclusion> <artifactId>junit</artifactId> <groupId>junit</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.1</version> <scope>test</scope> <optional>true</optional> <exclusions> <exclusion> <artifactId>mockito-all</artifactId> <groupId>org.mockito</groupId> </exclusion> </exclusions> </dependency> 
+1
source

Source: https://habr.com/ru/post/1243265/


All Articles