久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

JaCoCo + Mockito + Android 測試:報告的零覆蓋率

JaCoCo + Mockito + Android tests: Zero coverage reported(JaCoCo + Mockito + Android 測試:報告的零覆蓋率)
本文介紹了JaCoCo + Mockito + Android 測試:報告的零覆蓋率的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我知道這個主題有很多問題(和答案),但我已經嘗試了我在 SO 和其他網站上找到的所有內容,但我還沒有找到一種方法讓 JaCoCo 包含使用的 Android 測試的覆蓋范圍莫基托.

我的問題:我想使用 JaCoCo 生成單元測試和儀器測試(androidTest)的代碼覆蓋率.我正在使用 Mockito 來模擬一些課程.我在 GitHub 上找到了一個使用 JaCoCo 的示例,并以此為起點.

以及部分模擬的情況:

src/main/java/Util.java:

公共類 Util {整數獲取(){返回0;}int anIntMethod() {返回獲取();}}

src/test/java/UtilTest.java:

import org.junit.Test;導入 org.mockito.Mockito;導入靜態 org.junit.Assert.assertEquals;公共類 UtilTest {@測試公共無效 utilMethod() {Util util = Mockito.mock(實用程序類,Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));Mockito.doReturn(10).when(util).get();assertEquals(10, util.anIntMethod());}}

在這兩種情況下,模擬部分都顯示為未覆蓋,這是正確的.

I know there are quite a few questions (and answers) for this topic, but I've tried everything I found in SO and other sites and I haven't found a way to make JaCoCo include coverage for Android tests that use Mockito.

My problem: I want to use JaCoCo to generate code coverage of both Unit Test and Instrumentation Test (androidTest). I'm using Mockito to mock some of the classes. I found a sample in GitHub to use JaCoCo and used it as a starting point.

https://github.com/rafaeltoledo/unified-code-coverage-android

When I run the custom jacocoTestReport task included in that example, the code coverage report is properly generated and code coverage is at 100%. The report includes both unit test and android test. However, that sample is not using Mockito (which I need), so I added the following to app/build.gradle

dependencies {
 ...
 androidTestCompile 'org.mockito:mockito-android:2.10.0'
}

I added a very simple Java class called Util at app/src/main/java/net/rafaeltoledo/coverage/Util.java

public class Util {
    public int anIntMethod() {
        return 0;
    }
}

And added the following simple test to the existing android test at app/src/androidTest/java/net/rafaeltoledo/coverage/MainActivityTest.java

@Test
public void utilMethod() {
    Util util = Mockito.mock(Util.class);
    Mockito.doReturn(10).when(util).anIntMethod();
    assertThat(util.anIntMethod(), is(10));
}

When I run the jacocoTestReport again, code coverage drops to 88% and the report in fact shows the Util class was not covered by my tests, even though I clearly have a test that exercises that class.

(I wanted to add screenshots of the reports but I don't have enough reputation, so here's a link to the coverage report and execution report that shows that both tests were in fact executed)

Versions info: Gradle plug-in: 2.3.3 Jacoco: 0.7.8.201612092310 Android Studio: 2.3.3 Android build tools: 25.0.2

Is this a Jacoco limitation or am I doing something wrong?

解決方案

am I doing something wrong?

Let's put aside Android, because there is IMO clearly something wrong with your expectations/understanding about core thing here - mocking:

even though I clearly have a test that exercises that class.

By

@Test
public void utilMethod() {
    Util util = Mockito.mock(Util.class);
    Mockito.doReturn(10).when(util).anIntMethod();
    assertThat(util.anIntMethod(), is(10));
}

you are not testing anIntMethod, you are testing something that always returns 10, no matter what is actually written in anIntMethod.

Coverage shows what was executed and hence absolutely correct that it is zero for anIntMethod since it is not executed.

Mocking is used to isolate class under test from its dependencies, but not to replace it, otherwise you're not testing real code.

Here is an example of proper usage of mocking:

src/main/java/Util.java:

public class Util {
  int anIntMethod(Dependency d) {
    return d.get();
  }
}
class Dependency {
  int get() {
    return 0;
  }
}

src/test/java/UtilTest.java:

import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;

public class UtilTest {
  @Test
  public void utilMethod() {
    Dependency d = Mockito.mock(Dependency.class);
    Mockito.doReturn(10).when(d).get();
    assertEquals(10, new Util().anIntMethod(d));
  }
}

build.gradle:

apply plugin: "java"
apply plugin: "jacoco"

repositories {
  mavenCentral()
}

dependencies {
  testCompile "junit:junit:4.12"
  testCompile "org.mockito:mockito-core:2.10.0"
}

And after execution of gradle build jacocoTestReport coverage is

And case of partial mocking:

src/main/java/Util.java:

public class Util {
  int get() {
    return 0;
  }

  int anIntMethod() {
    return get();
  }
}

src/test/java/UtilTest.java:

import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;

public class UtilTest {
  @Test
  public void utilMethod() {
    Util util = Mockito.mock(
      Util.class,
      Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS)
    );
    Mockito.doReturn(10).when(util).get();
    assertEquals(10, util.anIntMethod());
  }
}

In both cases mocked parts are shown as uncovered and this is correct.

這篇關于JaCoCo + Mockito + Android 測試:報告的零覆蓋率的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現 IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 日韩午夜影院 | 欧美福利| 98久久 | 久久草在线视频 | jlzzjlzz国产精品久久 | 精品九九久久 | 一区二区国产在线观看 | 亚洲精品日本 | 精品综合视频 | 日韩中文字幕在线观看 | 中文字幕高清在线 | 日韩在线精品视频 | 午夜成人免费视频 | 免费观看一级毛片 | 国产精品视频久久久 | 日韩精品一区二区三区 | 国产精品欧美大片 | 久久夜视频 | 国产视频福利一区 | 精品在线 | 美日韩免费 | 免费一级淫片aaa片毛片a级 | 草草视频在线观看 | 亚洲人成免费 | 国产精品夜色一区二区三区 | 国产精品成人一区二区三区夜夜夜 | 国产综合精品 | 九九久久精品 | 久久一区二区av | 精品网站999 | 成年人免费在线视频 | 成人在线视频免费观看 | 九九综合| 亚洲国产精品自拍 | 欧美视频精品 | 黄色毛片免费看 | 一区视频在线 | 国产区久久 | 精品一区二区三区免费视频 | 午夜影院黄 | 日韩在线一区二区 |