如何将默认构造函数排除在Jacoco代码覆盖率检查之外?
如何排除Jacoco代码覆盖率检查中的默认构造函数?
你遇到的问题是Jacoco代码覆盖率检查时,默认构造函数ie.aviva.digital.salesgenerator.service.SalesService.SalesService()因行数超过预设最大值触发规则违反。要将其排除在检查范围外,可通过以下几种方式配置:
1. 精准排除指定类的默认构造函数
在Maven的jacoco-maven-plugin的check目标配置中,针对方法级别添加排除规则:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.10</version> <executions> <execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>METHOD</element> <excludes> <exclude>ie/aviva/digital/salesgenerator/service/SalesService.SalesService()</exclude> </excludes> <!-- 保留你原有的覆盖率规则配置 --> <limits> <limit> <counter>LINE</counter> <value>MAXIMUM</value> <maximum>35</maximum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin>
2. 全局排除所有默认构造函数
如果希望跳过所有类的默认构造函数检查,可使用通配符匹配所有构造方法(编译器生成的默认构造函数签名为<init>()):
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.10</version> <executions> <execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>METHOD</element> <excludes> <exclude>**/*.<init>()</exclude> </excludes> <!-- 你的覆盖率规则配置 --> </rule> </rules> </configuration> </execution> </executions> </plugin>
3. 排除整个类(可选)
如果该类的构造函数是唯一需要排除的部分,也可以直接排除整个类的检查:
<rule> <element>CLASS</element> <excludes> <exclude>ie/aviva/digital/salesgenerator/service/SalesService.class</exclude> </excludes> <!-- 你的覆盖率规则配置 --> </rule>
配置完成后重新执行Jacoco检查,即可跳过目标默认构造函数的行数校验。
内容的提问来源于stack exchange,提问作者runnerpaul
相关产品推荐
相关产品推荐

