最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于SpringBoot單元測試(cobertura生成覆蓋率報(bào)告)

 更新時(shí)間:2021年11月22日 16:33:18   作者:PC_Repair  
這篇文章主要介紹了關(guān)于SpringBoot單元測試(cobertura生成覆蓋率報(bào)告),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

demo(SpringBoot 項(xiàng)目)

被測試類:

import org.springframework.stereotype.Service;
@Service
public class TestService {
    public String sayHi() {
        return "hi";
    }
    public int divide(int a, int b) {
        return a / b;
    }
}

測試代碼:

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestServiceTest {
    @Autowired
    TestService testService;
    @Test
    public void testSayHi() {
        TestService testService = new TestService();
        String result = testService.sayHi();
        assertEquals("hi", result);
    }
    @Test
    public void testDivide() {
        TestService testService = new TestService();
        int result = testService.divide(3, 6);
        assertTrue(result > -1);
    }
}

pom.xml 配置文件

![cobertura](C:\Users\jiaflu\Desktop\cobertura.PNG)<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jiaflu</groupId>
    <artifactId>learn_springoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learn_springoot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <jackson.version>2.9.8</jackson.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

運(yùn)行mvn cobertura:cobertura 查看截圖:

覆蓋率測試報(bào)告生成(cobertura)

cobertura 原理

cobertura執(zhí)行過程大致如下:

  • 使用instrument修改我們編譯后的class文件,位于 target\generated-classes。
  • 執(zhí)行測試,測試數(shù)據(jù)輸出到xxx.ser中,位于 target\cobertura\cobertura.ser。
  • 使用report生成覆蓋率報(bào)告。

1.instrument

instrument:cobertura使用instrument修改我們編譯后的class文件,在代碼里面加入cobertura的統(tǒng)計(jì)代碼。并生成一個(gè).ser文件(用于覆蓋率數(shù)據(jù)的輸出)。

使用 instrument 執(zhí)行的過程中,CoberturaInstrumenter 會(huì)首先調(diào)用分析監(jiān)聽器分析給定的編譯好的.class,獲得touchPoint(可以認(rèn)為對應(yīng)于源代碼中的待覆蓋行)以及需要的其他信息。然后調(diào)用注入監(jiān)聽器將信息注入到新的.class中,保存到 \target\generated-classes 目錄下。

示例:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.cisco.webex.cmse.soa.soaservice.service;
import net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
import net.sourceforge.cobertura.coveragedata.TouchCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
@PropertySource({"classpath:application.properties"})
@Service
public class PropertyService implements HasBeenInstrumented {
    private static final Logger logger;
    @Value("${cdp.instance.url}")
    private String cdpInstanUrl;
    @Value("${soa.instance.url}")
    private String soaInstanceUrl;
    @Value("${github.api.token}")
    public String gitApiToken;
    @Value("${github.instance.url}")
    private String githubInstance;
    @Value("${github.repo.template.owner}")
    private String tplRepoOwner;
    @Value("${github.repo.consul.owner}")
    private String consulRepoOwner;
    @Value("${slm.listen.queue.name}")
    private String slmListenQueue;
    public PropertyService() {
        boolean var1 = false;
        int __cobertura__branch__number__ = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 12);
        super();
    }
    public String getCdpInstanUrl() {
        boolean var1 = false;
        int __cobertura__branch__number__ = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 33);
        return this.cdpInstanUrl;
    }
    ...
    public void setSlmListenQueue(String ()V) {
        boolean var2 = false;
        int __cobertura__branch__number__ = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 85);
        this.slmListenQueue = slmListenQueue;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 86);
    }
    static {
        boolean var0 = false;
        boolean var1 = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 13);
        logger = LoggerFactory.getLogger(PropertyService.class);
    }
}

2.執(zhí)行測試

在執(zhí)行測試用例時(shí),引用 cobertura 修改過的.class,測試信息寫入到cobertura.ser檔案文件。

3.生成報(bào)告

從cobertura.ser獲取覆蓋率數(shù)據(jù),然后結(jié)合src中提供的源代碼,生成最終的覆蓋率報(bào)告,放到了target\site\cobertura路徑下。若配置了生成 html 格式的報(bào)告,可以通過 index.html 查看覆蓋率測試報(bào)告。

SpringBoot pom.xml 配置

添加如下依賴:

       <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <!-- 此參數(shù)用于解決一個(gè)坑,下面會(huì)說明 -->
                    <argLine>-noverify</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <formats>
                        <format>xml</format>
                        <format>html</format>
                    </formats>
                </configuration>
            </plugin>

采坑:

在使用 mvn cobertura:cobertura 命令生成測試覆蓋率報(bào)告時(shí),出現(xiàn)了如下問題(截取部分,報(bào)錯(cuò)原因如下):

Reason:

Expected stackmap frame at this location.

Bytecode:

0x0000000: 2ab4 001b 2bb9 002e 0200 c600 2f2a b400

0x0000010: 1b2b b900 2e02 00c0 0030 b600 34c6 001c

解決方法:

本人使用的是 jdk1.8,添加 jvm 參數(shù) -noverify,可以在 pom 文件中添加配置,配置見上述 pom.xml

網(wǎng)上查資料 jdk1.7 添加 jvm 參數(shù) -XX:-UseSplitVerifier,(1.8沒有 -XX:-UseSplitVerifier 這參數(shù))

命令介紹

  • cobertura:check

根據(jù)最新的源碼標(biāo)記(生成的class文件)校驗(yàn)測試用例的覆蓋率,如果沒有達(dá)到要求,則執(zhí)行失敗.

  • cobertura:check-integration-test

這個(gè)命令和cobertura:check功能是一樣的,區(qū)別是二者綁定的maven生命周期不一樣.cobertura:check綁定了test, cobertura:check-integration-test綁定了verify.再說的明白些,maven生命周期中有一個(gè)是test跑得單元測試,還有一個(gè)是integration-test跑的集成測試.而verify前就是integration-test.即cobertura:check-integration-test比cobertura:check涵蓋的測試用例更多.

  • cobertura:clean

這個(gè)好理解,就是清理掉目錄/target/cobertura/中得文件.目前發(fā)現(xiàn)里面就一個(gè)文件cobertura.ser.

  • cobertura:cobertura

這個(gè)插件的關(guān)鍵命令.標(biāo)記被編譯的文件,運(yùn)行單元測試,生成測試報(bào)告.

  • cobertura:cobertura-integration-test

和cobertura:cobertura做了一樣的事情,區(qū)別是包含了集成測試用例.

  • cobertura:dump-datafile

在命令行輸出覆蓋率數(shù)據(jù).數(shù)據(jù)依據(jù)是生成的class文件.這個(gè)命令我沒搞懂他的意義何在.在后面一個(gè)有趣的實(shí)驗(yàn)我們會(huì)用這個(gè)命令來更好的理解cobertura-maven-plugin.

  • cobertura:help
  • cobertura:instrument

標(biāo)記被編譯的class文件.執(zhí)行這個(gè)命令會(huì)在目錄/target/generated-classes/cobertura下生成一套class文件.

maven-surefire-plugin 使用說明

Maven本身并不是一個(gè)單元測試框架,它只是在構(gòu)建執(zhí)行到特定生命周期階段的時(shí)候,通過插件來執(zhí)行JUnit或者TestNG的測試用例。這個(gè)插件就是maven-surefire-plugin,也可以稱為測試運(yùn)行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。

在默認(rèn)情況下,maven-surefire-plugin的test目標(biāo)會(huì)自動(dòng)執(zhí)行測試源碼路徑(默認(rèn)為src/test/java/)下所有符合一組命名模式的測試類。這組模式為:

  • */Test.java:任何子目錄下所有命名以Test開關(guān)的Java類。
  • */Test.java:任何子目錄下所有命名以Test結(jié)尾的Java類。
  • */TestCase.java:任何子目錄下所有命名以TestCase結(jié)尾的Java類。

maven-surefire-plugin 插件應(yīng)用:

1.跳過測試

跳過測試運(yùn)行 mvn package -DskipTests

或者通過 pom 提供該屬性:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.5</version>  
    <configuration>  
        <skipTests>true</skipTests>  
    </configuration>  
</plugin>

跳過測試代碼的編譯 mvn package -Dmaven.test.skip=true

或者通過 pom 提供該屬性:

<plugin>  
    <groupId>org.apache.maven.plugin</groupId>  
    <artifactId>maven-compiler-plugin</artifactId>  
    <version>2.1</version>  
    <configuration>  
        <skip>true</skip>  
    </configuration>  
</plugin>

2.動(dòng)態(tài)指定要運(yùn)行的測試用例

mvn test -Dtest=RandomGeneratorTest

也可以使用通配符:

mvn test -Dtest=Random*Test

或者也可以使用“,”號(hào)指定多個(gè)測試類:

mvn test -Dtest=Random*Test,AccountCaptchaServiceTest

如果沒有指定測試類,那么會(huì)報(bào)錯(cuò)并導(dǎo)致構(gòu)建失敗:

mvn test -Dtest

這時(shí)候可以添加 -DfailIfNoTests=false 參數(shù)告訴 maven-surefire-plugin 即使沒有任何測試也不要報(bào)錯(cuò):

mvn test -Dtest -DfailIfNoTests=false

3.包含與排除測試用例

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.5</version>  
    <configuration>  
        <includes>  
            <include>**/*Tests.java</include>  
        </includes>  
        <excludes>  
            <exclude>**/*ServiceTest.java</exclude>  
            <exclude>**/TempDaoTest.java</exclude>  
        </excludes>  
    </configuration>  
</plugin>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 看完這篇文章獲得一些java if優(yōu)化技巧

    看完這篇文章獲得一些java if優(yōu)化技巧

    if 是每個(gè)語言都有的語法,也是最基礎(chǔ)的語法。因?yàn)榇a本來就很晦澀,所以才有了程序員這個(gè)中間件,今天就聊一下我的一些關(guān)于 if 思路和總結(jié)
    2021-07-07
  • Java并發(fā)編程之threadLocal

    Java并發(fā)編程之threadLocal

    ThreadLocal是JDK包提供的,它提供了線程本地變量,也就是說如果創(chuàng)建了一個(gè)ThreadLocal變量,需要的朋友可以參考一下喲
    2021-09-09
  • SpringBoot關(guān)于List集合的校驗(yàn)方式

    SpringBoot關(guān)于List集合的校驗(yàn)方式

    這篇文章主要介紹了SpringBoot關(guān)于List集合的校驗(yàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring的同一個(gè)服務(wù)會(huì)加載多次的問題分析及解決方法

    Spring的同一個(gè)服務(wù)會(huì)加載多次的問題分析及解決方法

    這篇文章主要介紹了Spring的同一個(gè)服務(wù)為什么會(huì)加載多次,我們先來梳理一下?Web?容器中如何加載?Bean,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • 新的Java訪問mysql數(shù)據(jù)庫工具類的操作代碼

    新的Java訪問mysql數(shù)據(jù)庫工具類的操作代碼

    本文通過實(shí)例代碼給大家介紹新的Java訪問mysql數(shù)據(jù)庫工具類的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-12-12
  • 如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證

    如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證

    這篇文章主要介紹了如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證,本文給大家分享最新完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • 詳解Java中方法重寫和方法重載的6個(gè)區(qū)別

    詳解Java中方法重寫和方法重載的6個(gè)區(qū)別

    方法重寫和方法重載都是面向?qū)ο缶幊讨?,那么方法重寫和方法重載有哪些區(qū)別,本文就詳細(xì)的來介紹一下,感興趣的可以了解一下
    2022-01-01
  • Java正則驗(yàn)證IP的方法實(shí)例分析【測試可用】

    Java正則驗(yàn)證IP的方法實(shí)例分析【測試可用】

    這篇文章主要介紹了Java正則驗(yàn)證IP的方法,結(jié)合實(shí)例形式對比分析了網(wǎng)上常見的幾種針對IP的正則驗(yàn)證方法,最終給出了一個(gè)比較靠譜的IP正則驗(yàn)證表達(dá)式,需要的朋友可以參考下
    2017-08-08
  • JDBC編程的詳細(xì)步驟

    JDBC編程的詳細(xì)步驟

    這篇文章主要介紹了JDBC編程的詳細(xì)步驟,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)JDBC編程的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • 教你怎么用Springboot自定義Banner圖案

    教你怎么用Springboot自定義Banner圖案

    今天給大家?guī)淼氖荍ava的相關(guān)知識(shí),文章圍繞著怎么用Springboot自定義Banner圖案展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06

最新評論

涪陵区| 张掖市| 杭锦旗| 贺兰县| 赣榆县| 苍山县| 平江县| 息烽县| 筠连县| 永德县| 集贤县| 洮南市| 定安县| 始兴县| 湖南省| 双流县| 新乡县| 泗阳县| 偏关县| 武威市| 晴隆县| 中方县| 清水河县| 通河县| 抚顺县| 甘泉县| 谷城县| 平潭县| 萝北县| 景洪市| 济宁市| 贵南县| 泰宁县| 开远市| 张家口市| 阿拉尔市| 武冈市| 乌兰县| 榆社县| 德兴市| 阿克|