13

I'm using Robolectric to test my app, I decided to use Powermock to mock static and final classes so I added the following dependencies to my build.gradle file:

testImplementation "org.powermock:powermock-module-junit4:1.7.0"
testImplementation "org.powermock:powermock-module-junit4-rule:1.7.0"
testImplementation 'org.powermock:powermock-api-mockito2:1.7.0'
testImplementation 'org.powermock:powermock-classloading-xstream:1.7.0'

Then I started to write tests and everything works fine except that every test that uses Powermock prints the following warning too:

Properties file org/powermock/default.properties is found in 2 places: 
ConfigurationSource{location='file:/C:/Users/USUARIO/.gradle/caches/modules-2/files-2.1/org.powermock/powermock-core/1.7.0/19f022747953e7eccc3e53253f709d726931f407/powermock-core-1.7.0.jar!/org/powermock/default.properties}
ConfigurationSource{location='file:/C:/Users/USUARIO/.gradle/caches/modules-2/files-2.1/org.powermock/powermock-core/1.7.0/19f022747953e7eccc3e53253f709d726931f407/powermock-core-1.7.0.jar!/org/powermock/default.properties}
. Which one will be used is undefined. Please, remove duplicated configuration file (or second PowerMock jar file) from class path to have stable tests.Properties file org/powermock/default.properties is found in 2 places: 
ConfigurationSource{location='file:/C:/Users/USUARIO/.gradle/caches/modules-2/files-2.1/org.powermock/powermock-core/1.7.0/19f022747953e7eccc3e53253f709d726931f407/powermock-core-1.7.0.jar!/org/powermock/default.properties}
ConfigurationSource{location='file:/C:/Users/USUARIO/.gradle/caches/modules-2/files-2.1/org.powermock/powermock-core/1.7.0/19f022747953e7eccc3e53253f709d726931f407/powermock-core-1.7.0.jar!/org/powermock/default.properties}

. Which one will be used is undefined. Please, remove duplicated configuration file (or second PowerMock jar file) from class path to have stable tests.

Every test runs correctly however I want to get rid of that annoying warning message.

glisu
  • 1,027
  • 10
  • 20
  • 1
    To help others: I ran both of these commands and restarted my project and all of the warnings came back. It seems its a problem with Mockito itself rm -rf $HOME/.gradle/caches/ And ./gradlew cleanBuildCache – tommyboy Oct 24 '18 at 14:39

2 Answers2

8

TL;DR;

To fix it add "org.powermock.*" to @PowerMockIgnore, like:

@PowerMockIgnore({ "org.powermock.*", "org.mockito.*", "org.robolectric.*", "android.*", "androidx.*" })

In my case I've been using Roboelectric + PowerMock + Mockito2 and I got same issue, as PowerMock is loading itself few times to set everything up as using another Runner than PowerMock one.

I hope this helps someone :-)

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
forlayo
  • 1,371
  • 2
  • 13
  • 23
  • Could you share which versions you are using for robolectric, powermock and mockito2? Please share gradle dependecies, I'm getting some issues related to inter-compatibility issues. – Vikram Mar 23 '21 at 14:13
-1

I suggest you using scope test for Powermock

<dependency>
   <groupId>org.powermock</groupId>
    <artifactId>powermock-core</artifactId>
    <version>${powermock.version}</version>
    <scope>test</scope>
</dependency>