用PowerMock写的单元测试用例,sonar中覆盖率显示问题
- sonar中没有覆盖率的显示问题
- sonar中覆盖率显示不正确
- sonar中单元测试用例个数不正确问题
sonar中没有覆盖率的显示问题
pom文件中jacoco-maven-plugin配置不正确,做了如下配置,sonar中有了覆盖率的显示
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.9</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-prepare-agent-integration</id> <goals> <goal>prepare-agent-integration</goal> </goals> </execution> <execution> <id>default-report</id> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-report-integration</id> <goals> <goal>report-integration</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>BUNDLE</element> <limits> <limit> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.0</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin>
sonar中覆盖率显示不正确
- sonar中覆盖率的统计为0.0%1
PowerMock的版本从1.6.3改为1.6.6,覆盖率从0.0%变为有正常数据
sonar中统计的单元测试个数不正确
单元测试用例中多了很多名为setUp和tearDown的测试用例,按照正常的来说,这个函数不应包含在测试用例里面,报错如下:
Error Message
The class xx.xx.xx.xx.xx not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation. In case if you don't use this annotation, add the annotation on class or method level.
Stacktrace
org.powermock.api.mockito.ClassNotPreparedException:
The class xx.xx.xx.xx.xx not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
最后在@PrepareForTest()中添加了这个类之后,单元测试个数统计正确
@Test (groups = {"UT"})
@PrepareForTest({ClassName1.class,ClassName2.class})
@PowerMockIgnore({""})