码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-01-07 21:09:54      阅读:534      评论:0      收藏:0      [点我收藏+]

标签:cobertura   oid   lin   mojo   data   location   oal   for   private   

 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. public class UserOperator {
  6. @Bean
  7. public UserService userService() {
  8. UserService info = new UserService();
  9. return info;
  10. }
  11. }
  1. package junitdemo;
  2. import java.util.Date;
  3. public class UserService {
  4. public UserInfo userInfo() {
  5. UserInfo info = new UserInfo();
  6. info.setDate(new Date());
  7. info.setAge(333);
  8. info.setInfo("dddddd");
  9. return info;
  10. }
  11. }
  1. package junitdemo;
  2. import java.util.Date;
  3. public class UserInfo {
  4. private Date date;
  5. public Date getDate() {
  6. return date;
  7. }
  8. public void setDate(Date date) {
  9. this.date = date;
  10. }
  11. public String getInfo() {
  12. return info;
  13. }
  14. public void setInfo(String info) {
  15. this.info = info;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. private String info;
  24. private int 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. public class UserOperatorTest {
  10. @Autowired
  11. public UserService userService;
  12. @Test()
  13. public void 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 单元测试以及测试覆盖率

标签:cobertura   oid   lin   mojo   data   location   oal   for   private   

原文地址:http://www.cnblogs.com/rongfengliang/p/6260183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!