论坛首页 Java企业应用论坛

基于DBUnit的manage(Dao)单元测试

浏览 2693 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-07  
TestDBConnection父类 所有test类继承该类
package test.sample.service.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
/**
 * DbUnit 可以有不同的数据库操作,我使用了其中的两种:	  
	*DELETE_ALL ,它删除表中所有行。	   
	*CLEAN_INSERT ,它删除表中所有行并插入数据集提供的行。
	*This method inserts the contents of a FlatXmlDataSet file
*into the connection
 * @author donganlei
 *
 */
public class TestDBConnection extends DatabaseTestCase {
	private IDatabaseConnection conn;

	private String driverName = "oracle.jdbc.OracleDriver";

	private String dburl = "jdbc:oracle:thin:@192.168.0.2:1521:PORTAL";

	private String username = "portal";

	private String pwd = "portal";

	private String schema = "PORTAL";

	private String xmlUrl = "e:\\test.xml";//从数据库中取值到XML中(路径)

	private String dbxmlurl = "e:\\test.xml";//从XML中还原数据库中的记录(路径)
/**
 * 初始化TestDBConnection
 *
 */
	public TestDBConnection() {
		try {
			Class.forName(driverName);

			Connection jdbcConnection = DriverManager.getConnection(dburl,
					username, pwd);
			conn = new DatabaseConnection(jdbcConnection, schema);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
/**
 * 建立连接
 * 返回IDatabaseConnection
 */
	public synchronized IDatabaseConnection getConnection() throws Exception {
		Class.forName(driverName);
		Connection jdbcConnection = DriverManager.getConnection(dburl,
				username, pwd);
		conn = new DatabaseConnection(jdbcConnection, schema);
		return conn;
	}
	/**
	 * 方法:它删除表中所有行并插入数据集提供的行
	 * @throws Exception
	 */
     protected void insertFileIntoDb() throws Exception
      {
    	   DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
      } 
    /**
     * 方法:删除数据库中的所有数据
     * @throws Exception
     */   
    protected void deleteFileDb()throws Exception{
    	DatabaseOperation.DELETE_ALL.execute(conn, getDataSet());
    }
    /**
     * 方法 :从XML中读取数据
     */
	protected IDataSet getDataSet() throws Exception {
		return new FlatXmlDataSet(new FileInputStream(dbxmlurl));
	}
/**
 * 方法:读取数据库中的内容到xmlUrl中
 * @param tableNames
 * @throws Exception
 */
	public void backUp(String[] tableNames) throws Exception {
		IDataSet fullDataSet = conn.createDataSet(tableNames);
		FlatXmlDataSet.write(fullDataSet, new FileOutputStream(xmlUrl));
	}
	/**
	 * 方法:还原数据库中的数据
	 *
	 */
	public void overRead() {

	}
	/**
	 * 方法关闭连接
	 * @throws Exception
	 */
	public void closeConn()throws Exception{
		conn.close();
	}
}


Test类
package test.sample.service.manage;
import java.util.List;

import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;

import pub.tools.PageList;
import test.sample.service.util.TestDBConnection;

import com.huawei.service.manage.WordManage;
import com.huawei.service.object.LeaveWordObject;
/**
 *  insertFileIntoDb(): Inserts a file in the database 
	emptyTable(): Cleans a database table 
	insertAllFilesIntoDb(): Inserts all the files for your project 
	emptyAllTables(): Cleans all the tables for your project 

 * @author donganlei
 *
 */
public class WordManageTest  extends TestDBConnection {
	private IDataSet expectedDataSet;//XML中数据源设置
	private ITable expectedTable;//XML中的数据
	private IDataSet databaseDataSet;//数据库中的数据源设置
	private ITable actualTable;//数据库中的数据
	LeaveWordObject word;
	String[] args={"ser_leaveword"};//所有要操作的表
	private IDatabaseConnection conn;
	public WordManageTest()throws Exception{
		conn=this.getConnection();
		this.backUp(args);//得到数据
		expectedDataSet = getDataSet();
        expectedTable = expectedDataSet.getTable("ser_leaveword");
        databaseDataSet = getConnection().createDataSet();
        actualTable = databaseDataSet.getTable("ser_leaveword");
	}
	/**
	 * 测 getList 方法
	 * @throws Exception
	 */
	public void testGetWordList()throws Exception {
		PageList pagination=new PageList();
		WordManage wm=new WordManage();
		List list=wm.getWordList(1, pagination);
		String result=((LeaveWordObject)list.get(0)).getWordtitle();
        assertEquals("testGetWordList",expectedTable.getValue(15, "WORDTITLE"),actualTable.getValue(15, "WORDTITLE"));
        assertEquals("testGetWordList",expectedTable.getValue(15, "WORDTITLE"),result);
    }
	/**
	 * 测试Insert方法
	 * @throws Exception
	 */
	public void testInsertWord()throws Exception{
		WordManage wm=new WordManage();
		word=new LeaveWordObject();
		word.setClientid("1");
		word.setLeaveword("测试方法");
		word.setLeavetime("2008-01-01 11:11:11");
		word.setFlag("0");
		word.setAnswerman("3");
		word.setWordtitle("标题测试");
		wm.insertWord(word);
		conn=this.getConnection();
		this.backUp(args);
		assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"),actualTable.getValue(16, "WORDTITLE"));
		assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"),"标题测试");
	}
	/**
	 * 测试根据ID查找留言对象
	 * @throws Exception
	 */
	public void testGetWordById()throws Exception{
		WordManage wm=new WordManage();
		conn=this.getConnection();
		this.backUp(args);
		assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"),actualTable.getValue(16, "WORDTITLE"));
		assertEquals("testGetWordList",expectedTable.getValue(16, "WORDTITLE"), wm.getWordById(180).getWordtitle());
	}
	/**
	 * 根据用户ID和时间查询 测试用户留言列表
	 * @throws Exception
	 */
	public void testGetUserListByTime()throws Exception{
		WordManage wm=new WordManage();
		PageList pagination=new PageList();
		conn=this.getConnection();
		this.backUp(args);
		List list=wm.getUserListByTime(1, "2008-01-01 11:11:11", "2008-04-04 11:11:11", pagination);
		String result=((LeaveWordObject)list.get(0)).getWordtitle();
		
		assertEquals("testGetWordList",expectedTable.getValue(2, "WORDTITLE"),actualTable.getValue(2, "WORDTITLE"));
        assertEquals("testGetWordList",expectedTable.getValue(2, "WORDTITLE"),result);
	}
	/**
	 * 根据坐席ID和时间查询 测试用户留言列表
	 * @throws Exception
	 */
	public void testGetSeatListByTime()throws Exception{
		WordManage wm=new WordManage();
		PageList pagination=new PageList();
		conn=this.getConnection();
		this.backUp(args);
		List list=wm.getSeatListByTime(3, "2008-01-01 11:11:11", "2008-04-04 11:11:11", pagination);
		String result=((LeaveWordObject)list.get(0)).getWordtitle();
		assertEquals("testGetWordList",expectedTable.getValue(8, "WORDTITLE"),actualTable.getValue(8, "WORDTITLE"));
        assertEquals("testGetWordList",expectedTable.getValue(8, "WORDTITLE"),result);
	}
	/**
	 * 测试用户回复留言
	 * @throws Exception
	 */
	public void testUpdateWord()throws Exception{
		WordManage wm=new WordManage();
		word=new LeaveWordObject();
		word.setId("1");
		word.setClientid("1");
		word.setLeaveword("测试方法");
		word.setLeavetime("2008-01-01 11:11:11");
		word.setFlag("0");
		word.setAnswerman("3");
		word.setWordtitle("标题测试");
		word.setAnswercontent("测试回复");
		wm.updateWord(word);
		conn=this.getConnection();
		this.backUp(args);
		assertEquals("testGetWordList",expectedTable.getValue(0, "ANSWERCONTENT"),actualTable.getValue(0, "ANSWERCONTENT"));
		assertEquals("testGetWordList",expectedTable.getValue(0, "ANSWERCONTENT"),"测试回复");
	}
	
	public static void main(String[] args){
		junit.textui.TestRunner.run(WordManageTest.class);
	}
}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics