我正在使用Robolectric来测试我的应用程序,我决定使用Powermock来模拟静态和最终类,所以我将以下依赖项添加到我的build.gradle文件中:
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'
然后我开始编写测试,一切正常,但每次使用Powermock的测试都会打印以下警告:
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.
每个测试都正确运行,但我想摆脱那个烦人的警告信息。
TL; DR;
要修复它,请将“org.powermock。*”添加到@PowerMockIgnore,例如:
@PowerMockIgnore({ "org.powermock.*", "org.mockito.*", "org.robolectric.*", "android.*", "androidx.*" })
在我的情况下,我一直在使用Roboelectric + PowerMock + Mockito2,我遇到了同样的问题,因为PowerMock正在加载自己几次,所以使用另一个Runner而不是PowerMock。
我希望这可以帮助别人 :-)
我建议你使用Powermock的范围测试
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>