对于用Powermock编写的测试用例,sonar中单元测试覆盖率统计不正确的问题

用PowerMock写的单元测试用例,sonar中覆盖率显示问题

  • sonar中没有覆盖率的显示问题
  • sonar中覆盖率显示不正确
  • sonar中单元测试用例个数不正确问题

sonar中没有覆盖率的显示问题

  • pom文件中jacoco-maven-plugin配置不正确,做了如下配置,sonar中有了覆盖率的显示

         
                org.jacoco
                jacoco-maven-plugin
                0.7.9
                
                    
                        default-prepare-agent
                        
                            prepare-agent
                        
                    
                    
                        default-prepare-agent-integration
                        
                            prepare-agent-integration
                        
                    
                    
                        default-report
                        
                            report
                        
                    
                    
                        default-report-integration
                        
                            report-integration
                        
                    
                    
                        default-check
                        
                            check
                        
                        
                            
                                
                                    BUNDLE
                                    
                                        
                                            COMPLEXITY
                                            COVEREDRATIO
                                            0.0
                                        
                                    
                                
                            
                        
                    
                
            
    

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({""})

  1. 参考资料:https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo. ↩

你可能感兴趣的:(单元测试)