spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
Posted rongfengliang-荣锋亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring && Cobertura && maven &&junit 单元测试以及测试覆盖率相关的知识,希望对你有一定的参考价值。
1. 目的:
junit 单元测试,Cobertura 测试覆盖率报告
项目目录结构
2. maven 配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong.demoapp</groupId>
<artifactId>junitdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.release</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.release</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>xml</format>
</formats>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. spring 代码
package junitdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
publicclassUserOperator{
@Bean
publicUserService userService(){
UserService info =newUserService();
return info;
}
}
package junitdemo;
import java.util.Date;
publicclassUserService{
publicUserInfo userInfo(){
UserInfo info =newUserInfo();
info.setDate(newDate());
info.setAge(333);
info.setInfo("dddddd");
return info;
}
}
package junitdemo;
import java.util.Date;
publicclassUserInfo{
privateDate date;
publicDate getDate(){
return date;
}
publicvoid setDate(Date date){
this.date = date;
}
publicString getInfo(){
return info;
}
publicvoid setInfo(String info){
this.info = info;
}
publicint getAge(){
return age;
}
publicvoid setAge(int age){
this.age = age;
}
privateString info;
privateint age;
}
4. junit 测试代码
package junitdemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes =UserOperator.class)
publicclassUserOperatorTest{
@Autowired
publicUserService userService;
@Test()
publicvoid testUserOperator(){
for(int i =0; i <100; i++){
UserInfo info = userService.userInfo();
System.out.println(info.toString());
}
}
}
5. 生成测试报告
mvn cobertura:cobertura
6. 生成的报告
7. 扩展
集成jenkins 进行后期的统计,以及及时查看信息
以上是关于spring && Cobertura && maven &&junit 单元测试以及测试覆盖率的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot使用cobertura-maven-plugin生成代码覆盖率报表
当 cobertura 测试在 maven-cobertura-plugin 中运行时如何配置?
使用 maven 运行 junits 和 cobertura