我有一个小的示例Android应用程序,它是“开箱即用”的单个空活动应用程序。 当我尝试通过build.gradle添加powermock2时,它将无法运行。 下面是我的整个build.gradle文件。
注意:使用compile是因为我们要覆盖getSystemService(字符串服务)并在其中嵌入一些模拟来调整ConnectivityManger和NetworkType的返回值。
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.exampleapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
def powerMockVersion = '1.7.0'
dependencies {
repositories {
mavenCentral()
}
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ("org.powermock:powermock-module-junit4:$powerMockVersion") {
exclude group: 'org.objenesis', module: 'objenesis'
}
compile ('org.powermock:powermock-api-mockito2:1.7.0') {
exclude group: 'org.mockito', module: 'hamcrest-core'
exclude group: 'org.hamcrest', module: 'hamcrest-core'
exclude group: 'junit' exclude group: 'org.mockito'
exclude group: 'org.objenesis', module: 'objenesis'
}
// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
// })
// testCompile 'junit:junit:4.12'
}
重现:
在你的android项目中,
1)为空活动创建一个新的Android模块,并将其命名为exampleapp
2)为它编辑build.gradle文件。复制/粘贴以上内容
3)Gradle sync&跑吧。
因dexing错误而失败。
Error:Execution failed for task ':exampleapp:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lorg/powermock/api/mockito/expectation/WithExpectedArguments;
作为一种解决方法,如果我使用mockito(而不是mockito2),dex问题就会消失,但我需要mockito2。
compile ('org.powermock:powermock-api-mockito2:1.7.0') {...}
这可能是Powermock的一个缺陷。因此,我还在那里发布了一个问题...... https://github.com/powermock/powermock/issues/821
示例Android项目:http://23.79.234.224/Anaina/cache/TestData2/ficticious_project.tar
答案 0 :(得分:1)
注释掉junit,如下所示
dependencies {
repositories {
mavenCentral()
}
testCompile 'junit:junit:4.12'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ("org.powermock:powermock-module-junit4:$powerMockVersion") {
exclude group: 'org.objenesis', module: 'objenesis'
}
compile ('org.powermock:powermock-api-mockito2:1.7.0') {
exclude group: 'org.mockito', module: 'hamcrest-core'
exclude group: 'org.hamcrest', module: 'hamcrest-core'
exclude group: 'junit' exclude group: 'org.mockito'
exclude group: 'org.objenesis', module: 'objenesis'
}
// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
// })
}
了解更多支票this link
答案 1 :(得分:0)
所以似乎可以通过将它添加到build.gradle文件的android元素来克服第一个问题
dexOptions {
jumboMode true
multiDexEnabled true
}
一旦我这样做,我就不再遇到这篇文章中描述的问题了。相反,我遇到了java.beans的问题。
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy0.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.spy(Mockito.java:1812)
at com.akamai.android.sdk.internal.MapClientTestMainApp.getSystemService(MapClientTestMainApp.java:85)
at com.akamai.android.sdk.net.VocAccelerator.init(VocAccelerator.java:96)
at com.akamai.android.sdk.VocService.createVocService(VocService.java:143)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.setVocService(VocSdkLiveTest_BaseServiceTest.java:217)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.unRegisterIfNecessary(VocSdkLiveTest_BaseServiceTest.java:166)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.waitForSyncToFinishAndThenDeregister(VocSdkLiveTest_BaseServiceTest.java:211)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.unRegisterIfNecessary(VocSdkLiveTest_BaseServiceTest.java:151)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.setUp(VocSdkLiveTest_BaseServiceTest.java:41)
at com.akamai.android.sdk.internal.MapLT_AICTests.setUp(MapLT_AICTests.java:64)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:103)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1970)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
at org.powermock.configuration.support.ConfigurationMapper.map(ConfigurationMapper.java:45)
at org.powermock.configuration.support.ConfigurationReaderBuilder$ConfigurationReaderImpl.mapConfiguration(ConfigurationReaderBuilder.java:89)
at org.powermock.configuration.support.ConfigurationReaderBuilder$ConfigurationReaderImpl.createConfiguration(ConfigurationReaderBuilder.java:80)
at org.powermock.configuration.support.ConfigurationReaderBuilder$ConfigurationReaderImpl.read(ConfigurationReaderBuilder.java:71)
at org.powermock.configuration.support.ConfigurationFactoryImpl.readDefault(ConfigurationFactoryImpl.java:42)
at org.powermock.configuration.support.ConfigurationFactoryImpl.create(ConfigurationFactoryImpl.java:33)
at org.powermock.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:75)
at org.powermock.configuration.GlobalConfiguration.<init>(GlobalConfiguration.java:59)
at org.powermock.configuration.GlobalConfiguration.mockitoConfiguration(GlobalConfiguration.java:40)
at org.powermock.api.mockito.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:45)
at java.lang.Class.newInstance(Native Method)
at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:96)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:45)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:18)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:17)
at org.mockito.internal.configuration.plugins.Plugins.getMockMaker(Plugins.java:33)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
... 39 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.beans.Introspector" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.akamai.android.sdk.internal.test-1/base.apk", zip file "/data/app/com.akamai.android.sdk.internal-1/base.apk"],nativeLibraryDirectories=[/data/app/com.akamai.android.sdk.internal.test-1/lib/arm, /data/app/com.akamai.android.sdk.internal-1/lib/arm, /data/app/com.akamai.android.sdk.internal.test-1/base.apk!/lib/armeabi-v7a, /data/app/com.akamai.android.sdk.internal-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 56 more
Suppressed: java.lang.ClassNotFoundException: java.beans.Introspector
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 57 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
基于此,我认为Stack Overflow Post可能确实总结了为什么PowerMock在Android上的当前状态下工作。
也许这可以帮助别人......
答案 2 :(得分:0)
apparently powermock的powermock-api-mockito2模块及其直接依赖项org.powermock:powermock-api-mockito-common(均为版本1.7.4)为这些类声明.class文件:
我能够通过添加以下排除项来解决构建错误:
androidTestImplementation('org.powermock:powermock-api-mockito2:1.7.4') {
exclude group: 'org.powermock', module: 'powermock-api-mockito-common'
}
我不确定在此之后powermock的mockito API是否会工作,但是,因为删除整个模块实在是太过分了……我真的很想能够只删除dup类,但是我不能在现代gradle中找到一种方法。