Cobertura单元测试覆盖率实例

Cobertura 是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率。除了找出未测试到的代码并发现 bug 外,Cobertura 还可以通过标记无用的、执行不到的代码来优化代码,还可以提供 API 实际操作的内部信息。

Cobertura跟Maven完美结合,只需要在pom文件中增加一个插件即可:

<plugin>  
         <groupId>org.codehaus.mojo</groupId>  
         <artifactId>cobertura-maven-plugin</artifactId>  
         <version>2.7</version>  
</plugin> 

 

项目开发正确过程应该如此:

1.创建一个User对象:

package jmust.demo.Cobertura;

/**
 * Hello world!
 *
 */
public class User 
{
    private String userName;
    private String password;
    private String nickName;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getNickName() {
        return nickName;
    }
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    
    public User(){
        super();
    }
    
    public User(String userName,String password,String nickName){
        super();
        this.userName = userName;
        this.password = password;
        this.nickName = nickName;
    }
}

2.定义功能接口类:

package jmust.demo.Cobertura.Inter;

import jmust.demo.Cobertura.User;

public interface IUserService {

    public boolean add(User user);
    public boolean delete(String userName);
    public User load(String userName);
    public User login(String userName,String password);
}

3.编写单元测试类:

package jmust.demo.CoberturaTest;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import jmust.demo.Cobertura.User;
import jmust.demo.Cobertura.Exception.UserException;
import jmust.demo.Cobertura.Inter.IUserService;
import jmust.demo.Cobertura.Service.UserService;

/**
 * Unit test for simple User.
 */
public class UserServiceTest {
   private IUserService us;
   private User user;
   
   @Before
   public void setUp(){
       //初始化
       us = new UserService();
       user = new User("admin","123","管理员");
   }
   
   @Test
   public void addTest(){
       User u = user;
       us.add(u);
       User tu = us.load("admin");
       assertNotNull(tu);
   }
   
   @Test(expected=UserException.class)  
   public void AddExistUsername(){  
       us.add(user);  
       User tu = new User("admin", "1234", "alskdf");  
       boolean state = us.add(tu);  
       assertFalse(state);
   }  
     
   @Test  
   public void testDelete(){  
       us.add(user);  
       User tu = us.load(user.getUserName());  
       assertNotNull(tu);  
       us.delete(user.getUserName());  
       tu = us.load(user.getUserName());  
       assertNull(tu);  
   }  
     
   @Test  
   public void testLogin(){  
       us.add(user);  
       String username=user.getUserName();  
       String password=user.getPassword();  
       User tu = us.login(username, password);  
       assertNotNull(tu);
   }  
     
   @Test(expected=UserException.class)  
   public void testNotExistsUserLogin(){  
       us.add(user);  
       String username="admin1";  
       String password="123";  
       User tu = us.login(username, password);  
       assertNotNull(tu);
   }  
     
   @Test(expected=UserException.class)  
   public void testPasswordErrorUserLogin(){  
       us.add(user);  
       String username="admin";  
       String password="1235";  
       User tu = us.login(username, password);  
       assertNotNull(tu);
   }  
}

4.再编写功能实现类:

package jmust.demo.Cobertura.Service;

import java.util.HashMap;
import java.util.Map;

import jmust.demo.Cobertura.User;
import jmust.demo.Cobertura.Exception.UserException;
import jmust.demo.Cobertura.Inter.IUserService;

public class UserService implements IUserService{
    
    private Map<String,User> us = new HashMap<String,User>();
    /**
     * 添加
     */
    public boolean add(User user) {
        if(load(user.getUserName()) != null){
            throw new UserException("用户已存在");
        }
        us.put(user.getUserName(), user);
        return true;
    }
    /**
     * 删除
     */
    public boolean delete(String userName) {
        us.remove(userName);
        
        return true;
    }
    /**
     * 加载
     */
    public User load(String userName) {
        return us.get(userName);
    }
    /**
     * 登录
     */
    public User login(String userName, String password) {
        User u = load(userName);
        if(u == null) throw new UserException("用户不存在");
        if(!u.getPassword().equals(password)) throw new UserException("用户密码不正确");
        return u;
    }

}

 

 

 

到此,项目开发完成了。然后我们来执行,看看它的覆盖率如何:

首先,我们先来把项目打包成jar包,通过maven命令执行,  mvn install或者mvn package或者mvn deploy

然后,我们再继续执行maven命令,mvn cobertura:cobertura ,完成后,会在项目的目录target下有个site文件夹,我们打开里面的index页面,可以看到详细的覆盖率以及代码执行次数等。

 

 

 

posted @ 2015-11-28 13:36  吕坤  阅读(2333)  评论(0编辑  收藏  举报