jacoco + junit + mock 单测没有统计覆盖率问题
生活随笔
收集整理的這篇文章主要介紹了
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>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 单测没有统计覆盖率问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大班教案《太空》反思
- 下一篇: 苹果8和8p哪个值得入手