首页 > 代码库 > spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
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
public class UserOperator {
@Bean
public UserService userService() {
UserService info = new UserService();
return info;
}
}
package junitdemo;
import java.util.Date;
public class UserService {
public UserInfo userInfo() {
UserInfo info = new UserInfo();
info.setDate(new Date());
info.setAge(333);
info.setInfo("dddddd");
return info;
}
}
package junitdemo;
import java.util.Date;
public class UserInfo {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String info;
private int 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)
public class UserOperatorTest {
@Autowired
public UserService userService;
@Test()
public void 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 单元测试以及测试覆盖率
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。