本文介绍了使用的Mockito库Jacoco code覆盖Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序使用以下配置:

My android application uses the following config:

  • 在摇篮 - 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.

有没有人成功地生成Android应用程序code覆盖方法,它使用的Mockito库进行测试?

Has anyone managed to generate code coverage metrics for android application which uses mockito library for testing?

推荐答案

下面的链接是解释我遇到的问题非常有用:的

The following link was very useful in explaining the problem I encountered: http://www.androidpuzzles.com/168_17620080/

我随后改用源和目标兼容性设置为Java 1.5和我能够运行单元和UI测试(这同时使用的Mockito和ES preSSO),并使用Jacoco产生code覆盖报告。

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的设置,解决办法将是改变私有方法的范围,被测试的类要么保护或公开的范围。这会接着让我产生code覆盖报告(解决问题所确定的链接在内)。

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库Jacoco code覆盖Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:57