以前单纯用junit做spring的测试程序,在测dao的时候总是得手动的添加或删除记录,破坏了数据库现场,用spring的测试框架可以在测试用例执行完后回滚。
使用spring框架测试
引入spring测试的jar包(这里我用的是spring 3.0.3)
org.springframework.test-3.0.3.RELEASE.jar
cglib-2.2.2.jar
实体类
package com.jysd.bean;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.jysd.web.bean.ArticleActionParamBean;
@Entity
@Table(name="t_article")
public class Article {
//主键
private long id;
//板块
private Catagory catagory;
//主题
private String topic;
//文章内容
private String content;
//发布日期
private Date publis_date;
//文章作者
private User u;
/**
* 默认构造函数
*/
public Article(){
}
/**
*
* @param articleParambean 页面参数对象
*/
public Article(ArticleActionParamBean articleParambean){
Catagory catagory = new Catagory();
catagory.setId(Long.parseLong(articleParambean.getCatagory_id()));
this.catagory = catagory;
this.content = articleParambean.getContent();
this.topic = articleParambean.getTopic();
User u = new User();
u.setId(Long.parseLong(articleParambean.getUser_id()));
this.u = u;
Date d = new Date();
this.publis_date = d;
}
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="catagory_id")
public Catagory getCatagory() {
return catagory;
}
public void setCatagory(Catagory catagory) {
this.catagory = catagory;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Column(name="publish_date")
@Temporal(TemporalType.TIMESTAMP)
public Date getPublis_date() {
return publis_date;
}
public void setPublis_date(Date publisDate) {
publis_date = publisDate;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="userid")
public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
}
dao实现类
package com.jysd.dao.impl;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import com.jysd.bean.Article;
import com.jysd.dao.ArticleDao;
import com.jysd.exception.DataBaseException;
import com.jysd.ms.web.QueryResult;
@Repository
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements ArticleDao {
/**
* 功能:列出最近发表的文章
*/
public List<Article> listArticle(int displayCount,long catagory_id)throws DataBaseException {
// TODO Auto-generated method stub
/*LinkedHashMap<String,String> orderBy = new LinkedHashMap<String,String>();
orderBy.put("publis_date","desc");
QueryResult<Article> queryResult = this.getScrollData(0,displayCount,"where", queryParams, orderby)
return queryResult.getResultlist();*/
String hql = "select article from com.jysd.bean.Article as article where article.catagory.id=:id";
Query query = em.createQuery(hql).setFirstResult(0).setMaxResults(displayCount).setParameter("id",catagory_id);
return query.getResultList();
}
/**
* 功能:向数据库存储文
*/
public void saveArticle(Article article) throws DataBaseException {
em.persist(article);
}
测试类
package com.jysd.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import junit.framework.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.jysd.bean.Article;
import com.jysd.bean.Catagory;
import com.jysd.bean.User;
import com.jysd.dao.ArticleDao;
import com.jysd.exception.DataBaseException;
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring配置文件
@ContextConfiguration(locations={"classpath:bean.xml"})
public class ArticleDaoImplTest {
@Resource(name="articleDaoImpl")
private ArticleDao articleDao;
@Transactional //这个测试用例在一个事物中执行,执行完回回滚不会破坏数据库
public void testPostArticle() throws DataBaseException{
Article article = new Article();
article.setTopic("数据回滚");
article.setContent("学习spring测试");
User u = new User();
u.setId(1l);
article.setU(u);
Catagory cata = new Catagory();
cata.setId(1l);
article.setCatagory(cata);
articleDao.saveArticle(article);
}
}
bean.xml 如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="bjjysd"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
在运行这个测试用例,数据就不会保存在数据库中了,当然这只是spring测试框架的一部分,希望对你学习有所帮助。