使用Spring进行单元测试(上)

发表于:2013-6-05 11:03

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:赵才文    来源:51Testing软件测试网采编

  类总体介绍

  假设我们现在有一个基于 Spring 的应用程序,除了 MVC 层,还包括业务层和数据访问层,业务层有一个类 AccountService,负责处理账号类的业务,其依赖于数据访问层 AccountDao 类,此类提供了基于 Spring Jdbc Template 实现的数据库访问方法,AccountService 和 AccountDao 以及他们之间的依赖关系都是通过 Spring 配置文件进行管理的。

  现在我们要对 AccountService 类进行测试,在不使用 Spring 测试方法之前,我们需要这样做:

  清单 1. Account.Java

  此类代表账号的基本信息,提供 getter 和 setter 方法。

  1. package domain;  
  2.    
  3. public class Account {  
  4.     public static final String SEX_MALE = "male";  
  5.     public static final String SEX_FEMALE = "female";  
  6.    
  7.     private int id;  
  8.     private String name;  
  9.     private int age;  
  10.     private String sex;  
  11.     public String toString() {  
  12.        return String.format("Account[id=%d,name=%s,age:%d,sex:%s]",id,name,age,sex);  
  13.     }  
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.     public int getAge() {  
  27.         return age;  
  28.     }  
  29.     public void setAge(int age) {  
  30.         this.age = age;  
  31.     }  
  32.     public String getSex() {  
  33.         return sex;  
  34.     }  
  35.     public void setSex(String sex) {  
  36.         this.sex = sex;  
  37.     }  
  38.    
  39.     public static Account getAccount(int id,String name,int age,String sex) {  
  40.         Account acct = new Account();  
  41.         acct.setId(id);  
  42.         acct.setName(name);  
  43.         acct.setAge(age);  
  44.         acct.setSex(sex);  
  45.         return acct;  
  46.     }  
  47. }

  注意上面的 Account 类有一个 toString() 方法和一个静态的 getAccount 方法,getAccount 方法用于快速获取 Account 测试对象。

  清单 2. AccountDao.Java

  这个 DAO 我们这里为了简单起见,采用 Spring Jdbc Template 来实现。

  1. package DAO;  
  2.    
  3. import Java.sql.ResultSet;  
  4. import Java.sql.SQLException;  
  5. import Java.util.HashMap;  
  6. import Java.util.List;  
  7. import Java.util.Map;  
  8.    
  9. import org.Springframework.context.ApplicationContext;  
  10. import org.Springframework.context.support.ClassPathXmlApplicationContext;  
  11. import org.Springframework.jdbc.core.RowMapper;  
  12. import org.Springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;  
  13. import org.Springframework.jdbc.core.simple.ParameterizedRowMapper;  
  14.    
  15. import domain.Account;  
  16.    
  17. public class AccountDao extends NamedParameterJdbcDaoSupport {  
  18.     public void saveAccount(Account account) {  
  19.         String sql = "insert into tbl_account(id,name,age,sex) " +  
  20.                "values(:id,:name,:age,:sex)";  
  21.         Map paramMap = new HashMap();  
  22.         paramMap.put("id", account.getId());  
  23.         paramMap.put("name", account.getName());  
  24.         paramMap.put("age", account.getAge());  
  25.         paramMap.put("sex",account.getSex());  
  26.         getNamedParameterJdbcTemplate().update(sql, paramMap);  
  27.     }  
  28.    
  29.     public Account getAccountById(int id) {  
  30.         String sql = "select id,name,age,sex from tbl_account where id=:id";  
  31.         Map paramMap = new HashMap();  
  32.         paramMap.put("id", id);  
  33.         List<Account> matches = getNamedParameterJdbcTemplate().query(sql,  
  34.         paramMap,new ParameterizedRowMapper<Account>() {  
  35.                     @Override 
  36.                     public Account mapRow(ResultSet rs, int rowNum)  
  37.                             throws SQLException {  
  38.                         Account a = new Account();  
  39.                         a.setId(rs.getInt(1));  
  40.                         a.setName(rs.getString(2));  
  41.                         a.setAge(rs.getInt(3));  
  42.                         a.setSex(rs.getString(4));  
  43.                         return a;  
  44.                     }  
  45.    
  46.         });  
  47.         return matches.size()>0?matches.get(0):null;  
  48.     }  
  49.    
  50. }

  AccountDao 定义了几个账号对象的数据库访问方法:

  ● saveAccount:负责把传入的账号对象入库
  ● getAccountById:负责根据 Id 查询账号

42/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号