關(guān)于SpringBoot單元測試(cobertura生成覆蓋率報告)
demo(SpringBoot 項目)
被測試類:
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 配置文件
<?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>
運行mvn cobertura:cobertura 查看截圖:

覆蓋率測試報告生成(cobertura)
cobertura 原理
cobertura執(zhí)行過程大致如下:
- 使用instrument修改我們編譯后的class文件,位于 target\generated-classes。
- 執(zhí)行測試,測試數(shù)據(jù)輸出到xxx.ser中,位于 target\cobertura\cobertura.ser。
- 使用report生成覆蓋率報告。
1.instrument
instrument:cobertura使用instrument修改我們編譯后的class文件,在代碼里面加入cobertura的統(tǒng)計代碼。并生成一個.ser文件(用于覆蓋率數(shù)據(jù)的輸出)。
使用 instrument 執(zhí)行的過程中,CoberturaInstrumenter 會首先調(diào)用分析監(jiān)聽器分析給定的編譯好的.class,獲得touchPoint(可以認為對應(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í)行測試用例時,引用 cobertura 修改過的.class,測試信息寫入到cobertura.ser檔案文件。
3.生成報告
從cobertura.ser獲取覆蓋率數(shù)據(jù),然后結(jié)合src中提供的源代碼,生成最終的覆蓋率報告,放到了target\site\cobertura路徑下。若配置了生成 html 格式的報告,可以通過 index.html 查看覆蓋率測試報告。
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ù)用于解決一個坑,下面會說明 -->
<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 命令生成測試覆蓋率報告時,出現(xiàn)了如下問題(截取部分,報錯原因如下):
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ù)最新的源碼標記(生成的class文件)校驗測試用例的覆蓋率,如果沒有達到要求,則執(zhí)行失敗.
cobertura:check-integration-test
這個命令和cobertura:check功能是一樣的,區(qū)別是二者綁定的maven生命周期不一樣.cobertura:check綁定了test, cobertura:check-integration-test綁定了verify.再說的明白些,maven生命周期中有一個是test跑得單元測試,還有一個是integration-test跑的集成測試.而verify前就是integration-test.即cobertura:check-integration-test比cobertura:check涵蓋的測試用例更多.
cobertura:clean
這個好理解,就是清理掉目錄/target/cobertura/中得文件.目前發(fā)現(xiàn)里面就一個文件cobertura.ser.
cobertura:cobertura
這個插件的關(guān)鍵命令.標記被編譯的文件,運行單元測試,生成測試報告.
cobertura:cobertura-integration-test
和cobertura:cobertura做了一樣的事情,區(qū)別是包含了集成測試用例.
cobertura:dump-datafile
在命令行輸出覆蓋率數(shù)據(jù).數(shù)據(jù)依據(jù)是生成的class文件.這個命令我沒搞懂他的意義何在.在后面一個有趣的實驗我們會用這個命令來更好的理解cobertura-maven-plugin.
cobertura:helpcobertura:instrument
標記被編譯的class文件.執(zhí)行這個命令會在目錄/target/generated-classes/cobertura下生成一套class文件.
maven-surefire-plugin 使用說明
Maven本身并不是一個單元測試框架,它只是在構(gòu)建執(zhí)行到特定生命周期階段的時候,通過插件來執(zhí)行JUnit或者TestNG的測試用例。這個插件就是maven-surefire-plugin,也可以稱為測試運行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。
在默認情況下,maven-surefire-plugin的test目標會自動執(zhí)行測試源碼路徑(默認為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.跳過測試
跳過測試運行 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.動態(tài)指定要運行的測試用例
mvn test -Dtest=RandomGeneratorTest
也可以使用通配符:
mvn test -Dtest=Random*Test
或者也可以使用“,”號指定多個測試類:
mvn test -Dtest=Random*Test,AccountCaptchaServiceTest
如果沒有指定測試類,那么會報錯并導(dǎo)致構(gòu)建失敗:
mvn test -Dtest
這時候可以添加 -DfailIfNoTests=false 參數(shù)告訴 maven-surefire-plugin 即使沒有任何測試也不要報錯:
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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
5種必會的Java異步調(diào)用轉(zhuǎn)同步的方法你會幾種
這篇文章主要介紹了5種必會的Java異步調(diào)用轉(zhuǎn)同步的方法你會幾種,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
SpringCloud服務(wù)實現(xiàn)同時使用eureka和nacos方法
這篇文章主要介紹了SpringCloud服務(wù)實現(xiàn)同時使用eureka和nacos方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01
Java迭代器實現(xiàn)Python中的range代碼實例
這篇文章主要介紹了Java迭代器實現(xiàn)Python中的range代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
springboot整合logback實現(xiàn)日志管理操作
本章節(jié)是記錄logback在springboot項目中的簡單使用,本文將會演示如何通過logback將日志記錄到日志文件或輸出到控制臺等管理操作,感興趣的朋友跟隨小編一起看看吧2024-02-02
淺談SpringCloud的微服務(wù)架構(gòu)組件
這篇文章主要介紹了淺談SpringCloud的微服務(wù)架構(gòu)組件,Spring Cloud根據(jù)分布式服務(wù)協(xié)調(diào)治理的需求成立了許多子項目,每個項目通過特定的組件去實現(xiàn),需要的朋友可以參考下2023-04-04
Java后端限制頻繁請求和重復(fù)提交的實現(xiàn)
很多用戶會請求過于頻繁或者是多次重復(fù)提交數(shù)據(jù),本文主要介紹了Java后端限制頻繁請求和重復(fù)提交的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

