【发布时间】:2017-07-19 11:46:03
【问题描述】:
我编写了一个小型示例 Android 应用程序,它是“开箱即用”的单个空活动应用程序。 当我尝试通过 build.gradle 添加 powermock2 时,它不会运行。 下面是我的整个 build.gradle 文件。
注意:使用 compile 是因为我们将覆盖 getSystemService(String service) 并在其中嵌入一些模拟来调整 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'
}
复制:
在你的安卓项目中,
1) 为空活动创建一个新的 Android 模块并将其命名为 exampleapp。
2) 为它编辑你的 build.gradle 文件。复制/粘贴以上内容。
3) Gradle 同步并运行它。
因 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
【问题讨论】:
-
点击 Build 然后 Clean Project 并等待该过程完成。之后,再次点击Build,然后点击Rebuild Project。
标签: android junit4 powermock powermockito