Jacoco code coverage for android application using mockito library(使用 mockito 库的 android 应用程序的 Jacoco 代码覆盖率)
问题描述
我的 android 应用程序使用以下配置:
My android application uses the following config:
- Gradle - 0.12.+
build.gradle 文件的内容
Contents of build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: 'com.android.library'
apply plugin: "jacoco"
dependencies {
compile 'commons-collections:commons-collections:3.2.1'
compile 'org.slf4j:slf4j-android:1.6.1-RC1'
// dependency injection
compile('org.roboguice:roboguice:2.0') {
exclude module: 'cglib'
exclude module: 'aopalliance'
exclude module: 'guice'
}
compile files('libs/guice-3.0-no_aop.jar')
compile 'javax.inject:javax.inject:1'
/*
* Test dependencies.
*/
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
}
android {
buildToolsVersion "20.0"
compileSdkVersion 19
buildTypes {
debug {
runProguard false
testCoverageEnabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 100
versionName "1.0.0"
}
/*
* Workaround for packaging bug in Android Gradle plugin regarding duplicate files.
*/
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'LICENSE.txt'
}
}
当属性 testCoverageEnabled 设置为 false 时,我的所有测试都会成功运行.将其设置为 true 时,运行测试时会引发以下异常
All my tests run successfully when property testCoverageEnabled is set to false. On setting it to true, the following exception is thrown when running the tests
Caused by: java.lang.VerifyError: *** Some class ***
at dalvik.system.DexFile.defineClass(Native Method)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:211)
at dalvik.system.DexPathList.findClass(DexPathList.java:313)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:51)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
当在测试中初始化模拟时,错误发生在行上.
The error happens on the line when mocks are initialised within the tests.
有没有人设法为使用 mockito 库进行测试的 android 应用程序生成代码覆盖率指标?
Has anyone managed to generate code coverage metrics for android application which uses mockito library for testing?
推荐答案
以下链接对我遇到的问题非常有用:http://www.androidpuzzles.com/168_17620080/
The following link was very useful in explaining the problem I encountered: http://www.androidpuzzles.com/168_17620080/
随后我将源和目标兼容性设置切换到 Java 1.5,并且能够运行单元和 UI 测试(使用 mockito 和 espresso)并使用 Jacoco 生成代码覆盖率报告.
I subsequently switched the source and target compatibility settings to Java 1.5 and I was able to run the unit and UI tests (which used both mockito and espresso) and generate code coverage report using Jacoco.
如果我必须保留 Java 1.7 设置,解决方法是将被测试类中私有方法的范围更改为受保护或公共范围.这将允许我生成代码覆盖率报告(克服包含的链接中确定的问题).
If I had to retain Java 1.7 settings, the workaround would have been to change the scope of private methods in the class being tested to either protected or public scope. This would have then allowed me to generate the code coverage report (overcoming the issue as identified in the link included).
这篇关于使用 mockito 库的 android 应用程序的 Jacoco 代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 mockito 库的 android 应用程序的 Jacoco 代码覆盖


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01