How to get Powermock to work with Dexmaker

I am trying to enable Powermock as a dependency for my Android tests using the following build.gradle configuration:

dependencies{ compile 'com.android.support:appcompat-v7:21.0.+' androidTestCompile('org.mockito:mockito-core:1.9.5') androidTestCompile('com.google.dexmaker:dexmaker:1.2') androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.2') androidTestCompile('org.powermock:powermock-module-junit4:1.5.5') { exclude module: 'junit' } androidTestCompile('org.powermock:powermock-api-mockito:1.5.5') { exclude module: 'mockito-all' } } 

However, the compiler complains that

 Error:Gradle: Execution failed for task ':app:packageDebugTest'. > Duplicate files copied in APK mockito-extensions/org.mockito.plugins.MockMaker File 1: ~/.gradle/caches/modules-2/files-2.1/com.google.dexmaker/dexmaker-mockito/1.2/b99884a4c6ef6335ba376f79aa79632b2421c17c/dexmaker-mockito-1.2.jar File 2: ~/.gradle/caches/modules-2/files-2.1/com.google.dexmaker/dexmaker-mockito/1.2/b99884a4c6ef6335ba376f79aa79632b2421c17c/dexmaker-mockito-1.2.jar 

Looking into the jar framework, I noticed that both Dexmaker and Powermock declare MockMaker in mockito-extensions

img

What is MockMaker? How do they differ? And the most important question: is it possible to get Powermock to work well with Dexmaker?

Thanks in advance. Any help would be greatly appreciated.

+7
java android mockito powermock dexmaker
source share
3 answers

MockMaker is an adhesive module that combines dexmaker with Mockito. It does what Mockito needs to create specific classes with Dalvik.dex files instead of JVM.class files.

It is possible that Powermock will work with Dexmaker, but it is unlikely that the advanced features of Powermock will work. In particular, Powermock touts this :

PowerMock uses custom loading of classes and bytecode to enable mockery of static methods, constructors, final classes and methods, private methods, removing static initializers, etc.

This custom classloader is unlikely to work on dalvikvm.

+9
source share

you can try putting this in your build.gradle, it solved the same problem for me

 android{ ... packagingOptions{ exclude 'mockito-extensions/org.mockito.plugins.MockMaker' } ... } 
+3
source share

I had the same problem and found a solution here . This is due to a bit of manual work, and you will have to modify the jar file yourself.

So what nparihar offers is the following.

  • Make a backup copy of powermock-api-mockito-1.5.5.jar file
  • Rename powermock-api-mockito-1.5.5.jar to powermock-api-mockito-1.5.5.zip
  • Unzip powermock-api-mockito-1.5.5.zip
  • cd powermock-api-mockito-1.5.5 /
  • rm -rf mockito-extensions
  • jar cf powermock-api-mockito-1.5.5.jar META-INF / org /
  • add a new jar to the libs file.

This solution worked for me. Let me know if this works for you.

In addition, I see that we are using the same dependencies. In my case, I had to manually delete hamcrest.jar and objenesis.jar, as there were confitures.

Hope this helps.

+1
source share

All Articles