日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jacoco + junit + mock 单测没有统计覆盖率问题

發布時間:2023/12/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jacoco + junit + mock 单测没有统计覆盖率问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用junit :直接在pom文件中直接引入:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><epps-pccs.version>1.0.0</epps-pccs.version><hibernate-validator.version>4.2.0.Final</hibernate-validator.version><mybatis.version>3.0.6</mybatis.version><mybatis-spring.version>1.0.2</mybatis-spring.version><dbcp.version>1.2.2</dbcp.version><cxf-version>2.6.1</cxf-version><xstream.version>1.4.3</xstream.version><junit.version>4.8.1</junit.version> ......<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.1</version><scope>test</scope></dependency>
引入mock:

<tcc.version>0.0.5</tcc.version><epps-task.version>2.0.0-SNAPSHOT</epps-task.version><mockito-all.version>2.0.2-beta</mockito-all.version><sonar.coverage.exclusions>**/basis/*,**/cache/*,**/client/*,**/exception/*,**/general/*,**/rsf/*,**/scm/*,**/service/*,**/task/*,**/uts/*</sonar.coverage.exclusions> <dependency><groupId>org.mockito</groupId><artifactId>mockito-all</artifactId><version>${mockito-all.version}</version><scope>test</scope></dependency>
然后引入jacoco:

<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.7.201606060606</version> <configuration> <skip>${jacoco.skip.instrument}</skip> </configuration> <executions><execution> <id>pre-unit-test</id> <goals><goal>prepare-agent</goal> </goals> <configuration> <dataFile>${project.build.directory}/jacoco.exec</dataFile> <propertyName>surefireArgLine</propertyName> </configuration> </execution> <execution> <id>default-instrument</id> <goals> <goal>instrument</goal></goals> </execution><execution> <id>default-restore-instrumented-classes</id> <goals> <goal>restore-instrumented-classes</goal></goals> </execution> <execution> <id>report</id> <goals> <goal>report</goal></goals> <phase>prepare-package</phase><configuration> <dataFile>${project.build.directory}/jacoco.exec</dataFile></configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <systemPropertyVariables> <jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> <argLine>-noverify -XX:-UseSplitVerifier -XX:NewSize=128m -XX:MaxNewSize=512m -XX:PermSize=128m -XX:MaxPermSize=768m ${surefireArgLine} </argLine></configuration> </plugin> </plugins>

對于junit 沒有統計到覆蓋率:
多出:

<dependency><!--<groupId>org.powermock</groupId>--><!--<artifactId>powermock-module-junit4</artifactId>--><!--<version>${powermock.version}</version>--><!--<scope>test</scope>--></dependency><dependency><!--<groupId>org.powermock</groupId>--><!--<artifactId>powermock-api-mockito</artifactId>--><!--<version>${powermock.version}</version>--><!--<scope>test</scope>--></dependency><dependency><!--<groupId>org.powermock</groupId>--><!--<artifactId>powermock-module-junit4-rule-agent</artifactId>--><!--<version>${powermock.module.rule.agent.version}</version>--><!--<scope>test</scope>--></dependency><dependency><!--<groupId>org.powermock</groupId>--><!--<artifactId>powermock-core</artifactId>--><!--<version>${powermock.version}</version>--><!--<scope>test</scope>--> </dependency>
還有如果按模塊單測,那么每一個模塊都要單獨引入:

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>

#####################################################################################################

正常被jacoco統計的junit +mock 單元代碼:

import com.alibaba.fastjson.JSONObject; import com.***.epp.pccs.common.enums.BusinessCodeEnum; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.MockitoAnnotations;/*** */ public class LogWrapperTest {@InjectMocksprivate LogWrapper logWrapper;@Beforepublic void initMocks() {MockitoAnnotations.initMocks(this);}@Testpublic void buildMonitorLogString_test() {JSONObject req = new JSONObject();req.put("payAmount", "123.45");LogWrapper.buildMonitorLogString(req, new JSONObject(), "1234567890", BusinessCodeEnum.DEPOSIT, "120");}}

***********************************************對于沒有統計到的單測**********************************************************

pom文件里多出了testng插件:

<dependency><!--<groupId>org.testng</groupId>--><!--<artifactId>testng</artifactId>--><!--<version>6.4</version>--><!--<scope>test</scope>--></dependency>








總結

以上是生活随笔為你收集整理的jacoco + junit + mock 单测没有统计覆盖率问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。