spring && Cobertura && maven &&junit 单元测试以及测试覆盖率

Posted rongfengliang-荣锋亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring && Cobertura && maven &&junit 单元测试以及测试覆盖率相关的知识,希望对你有一定的参考价值。

 1. 目的:

 
      junit 单元测试,Cobertura   测试覆盖率报告
      项目目录结构
      
 
2. maven 配置
   
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.dalong.demoapp</groupId>
  5. <artifactId>junitdemo</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-test</artifactId>
  11. <version>4.3.5.release</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>4.12</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-web</artifactId>
  21. <version>4.3.5.release</version>
  22. </dependency>
  23. </dependencies>
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.codehaus.mojo</groupId>
  28. <artifactId>cobertura-maven-plugin</artifactId>
  29. <version>2.7</version>
  30. <configuration>
  31. <formats>
  32. <format>xml</format>
  33. </formats>
  34. </configuration>
  35. <executions>
  36. <execution>
  37. <phase>package</phase>
  38. <goals>
  39. <goal>cobertura</goal>
  40. </goals>
  41. </execution>
  42. </executions>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>
 
3. spring 代码
 
   
  1. package junitdemo;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. publicclassUserOperator{
  6. @Bean
  7. publicUserService userService(){
  8. UserService info =newUserService();
  9. return info;
  10. }
  11. }
  1. package junitdemo;
  2. import java.util.Date;
  3. publicclassUserService{
  4. publicUserInfo userInfo(){
  5. UserInfo info =newUserInfo();
  6. info.setDate(newDate());
  7. info.setAge(333);
  8. info.setInfo("dddddd");
  9. return info;
  10. }
  11. }
  1. package junitdemo;
  2. import java.util.Date;
  3. publicclassUserInfo{
  4. privateDate date;
  5. publicDate getDate(){
  6. return date;
  7. }
  8. publicvoid setDate(Date date){
  9. this.date = date;
  10. }
  11. publicString getInfo(){
  12. return info;
  13. }
  14. publicvoid setInfo(String info){
  15. this.info = info;
  16. }
  17. publicint getAge(){
  18. return age;
  19. }
  20. publicvoid setAge(int age){
  21. this.age = age;
  22. }
  23. privateString info;
  24. privateint age;
  25. }
4. junit 测试代码
   
  1. package junitdemo;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @ContextConfiguration(classes =UserOperator.class)
  9. publicclassUserOperatorTest{
  10. @Autowired
  11. publicUserService userService;
  12. @Test()
  13. publicvoid testUserOperator(){
  14. for(int i =0; i <100; i++){
  15. UserInfo info = userService.userInfo();
  16. System.out.println(info.toString());
  17. }
  18. }
  19. }
5. 生成测试报告
 
  1. mvn cobertura:cobertura
6. 生成的报告
 
   
 
7. 扩展
 
     集成jenkins 进行后期的统计,以及及时查看信息
 
  
 

以上是关于spring && Cobertura && maven &&junit 单元测试以及测试覆盖率的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot使用cobertura-maven-plugin生成代码覆盖率报表

当 cobertura 测试在 maven-cobertura-plugin 中运行时如何配置?

使用 maven 运行 junits 和 cobertura

从 gradle 向配置了 cobertura 的 sonarqube 报告覆盖率

使用 Cobertura 从代码覆盖中排除方法

SONAR - 使用 Cobertura 测量代码覆盖率