spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的:
<project xmlns="http:///POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="http:///POM/4.0.0 http:///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>
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){
= info;
}
publicint getAge(){
return age;
}
publicvoid setAge(int age){
this.age = age;
}
privateString info;
privateint age;
}
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());
}
}
}
mvn cobertura:cobertura