pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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 庫的傳遞依賴)
主站蜘蛛池模板: 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 橡胶接头_橡胶软接头_可曲挠橡胶接头-巩义市创伟机械制造有限公司 | 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | sfp光模块,高速万兆光模块工厂-性价比更高的光纤模块制造商-武汉恒泰通 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 高压互感器,电流互感器,电压互感器-上海鄂互电气科技有限公司 | 电子书导航网_电子书之家_电子书大全_最新电子书分享发布平台 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 北京模型公司-军事模型-工业模型制作-北京百艺模型沙盘公司 | 淘剧影院_海量最新电视剧,免费高清电影随心观看 | 煤矿人员精确定位系统_矿用无线通信系统_煤矿广播系统 | 西安烟道厂家_排气道厂家_包立管厂家「陕西西安」推荐西安天宇烟道 | 广西教师资格网-广西教师资格证考试网| 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 板式换网器_柱式换网器_自动换网器-郑州海科熔体泵有限公司 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 一氧化氮泄露报警器,二甲苯浓度超标报警器-郑州汇瑞埔电子技术有限公司 | 铝扣板-铝方通-铝格栅-铝条扣板-铝单板幕墙-佳得利吊顶天花厂家 elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | loft装修,上海嘉定酒店式公寓装修公司—曼城装饰| 电车线(用于供电给电车的输电线路)-百科 | 工业用品一站式采购平台|南创工品汇-官网|广州南创 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 东莞市海宝机械有限公司-不锈钢分选机-硅胶橡胶-生活垃圾-涡电流-静电-金属-矿石分选机 | 武汉高低温试验箱_恒温恒湿试验箱厂家-武汉蓝锐环境科技有限公司 | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 湿地保护| 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 地埋式垃圾站厂家【佳星环保】小区压缩垃圾中转站转运站 | 粘度计,数显粘度计,指针旋转粘度计 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 |