spring-mock单元测试新方法

在进行dao的集成测试时候,数据清理,察看数据都是比较麻烦的事情,使用Spring-mock.jar可以帮助我们简化着一个过程。
我举一个简单的例子,说明一下如何使用spring-mock。 

首先是po, hbm.xml, dao, daoimpl没什么好说的: 

Customer.java : 

package rst.spring.mock; 

import java.io.Serializable; 

/** @author Hibernate CodeGenerator */ 
public class Customer implements Serializable { 

   /** identifier field */ 
   private Long id; 

   /** nullable persistent field */ 
   private String name; 

   /** full constructor */ 
   public Customer(String name) { 
       this.name = name; 
   } 

   /** default constructor */ 
   public Customer() { 
   } 

   public Long getId() { 
       return this.id; 
   } 

   public void setId(Long id) { 
       this.id = id; 
   } 

   public String getName() { 
       return this.name; 
   } 

   public void setName(String name) { 
       this.name = name; 
   } 



Customer.hbm.xml : 

 
       "-//Hibernate/Hibernate Mapping DTD//EN" 
       "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> 
 
 
 
 
 
 
 

 

CustomerDAO : 
/* 
* Created on 2005-3-25 
*/ 
package rst.spring.mock; 

import org.springframework.dao.DataAccessException; 

/** 
* @author rst 

*/ 
public interface CustomerDAO { 
   public void add(Customer customer) throws DataAccessException; 


CustomerDAOImpl : 

package rst.spring.mock; 

import org.springframework.dao.DataAccessException; 
import org.springframework.orm.hibernate.support.HibernateDaoSupport; 

/** 
* Class description. 

* @author rst 
*/ 
public class CustomerDAOHibernateImpl extends HibernateDaoSupport implements CustomerDAO{ 
    
   public void add(Customer customer) throws DataAccessException{ 
       this.getHibernateTemplate().save(customer); 
   } 




然后测试的基类SpringDAOTestCase继承自AbstractTransactionalDataSourceSpringContextTests,目前只有一个指定测试用xml文件位置的逻辑。 

package rst.spring.mock; 

import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; 

/** 
* Class description. 

* @author rst 
*/ 
public abstract class SpringDAOTestCase extends AbstractTransactionalDataSourceSpringContextTests { 

 protected String[] getConfigLocations() { 
   return new String[] { "test.xml" }; 
 } 




接着是我们真正测试的类CustomerDAOTest.java: 

package rst.spring.mock; 

/** 
* Class description. 

* @author rst 
*/ 
public class CustomerDaoTest extends SpringDAOTestCase { 

   private CustomerDAOHibernateImpl customerDAO; 

   protected void onSetUpInTransaction() throws Exception { 
       super.onSetUpInTransaction(); 
       //this.setPopulateProtectedVariables(true); 
       customerDAO = (CustomerDAOHibernateImpl) this.applicationContext.getBean("customerDAO"); 
   } 

   protected void onTearDownInTransaction() { 
       customerDAO = null; 
   } 

   public void testInsert() { 
       Customer customer = new Customer(); 
       customer.setName("javaeye"); 
       customerDAO.add(customer); 
       String name = (String) jdbcTemplate.queryForObject("select name from customer where id=?", new Object[]{customer.getId()}, String.class); 
       
       assertEquals(customer.getName(), name); 
   } 




最后看看配置文件test.xml: 

 
 

 
 


  
 
 
 
classpath:jdbc.properties 
 

 
 
${jdbc.driverClassName} 
${jdbc.url} 
${jdbc.username} 
${jdbc.password} 
 

 
 
 
 
rst/spring/mock/Customer.hbm.xml 
 
 
 
${hibernate.dialect} 
true 
 
 
 

 
 
 
 

 
 
 

 
 
 
 

这个文件很简单,不要忘记transactionManager的配置,Test类会自动装配的。 

运行之后,就可以看到应有的结果,并且数据库中不会有数据污染。这个过程主要是开始一个transaction,然后开始你的test方法,执行dao操作,
执行sql查询验证结果,最后无论成功失败rollback transaction。

你可能感兴趣的:(spring-mock单元测试新方法)