日期:2014-05-16  浏览次数:20451 次

SC测试总结(二)--数据库测试DBunitDemo
测试结构如下:
1  定义测试数据
2  将测试数据导入到数据库中
3  将预期结果和实际结果进行对比

目的:
测试selectAllDemo方法,检测查询结果是否有效

使用DBuint框架完成测试
返回测试用的数据库连接对象
protected abstract IDatabaseConnection getConnection() throws Exception;
返回测试用XML数据集对象
protected abstract IDataSet getDataSet() throws Exception;

部分参考csdn文章

package baor.src.dbunit;

import java.sql.ResultSet;
import java.sql.SQLException;
import baor.test.sql.ConnPropInfo;
import baor.test.sql.ConnUtil;
import java.sql.Statement;

public interface CRUDDemo {
	public abstract ResultSet selectAllDemo(Statement stmt, String SQLString)
			throws SQLException;
	public abstract Statement setSTMT(ConnUtil connUtil, ConnPropInfo demo)
			throws SQLException;
}



package baor.src.dbunit;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import baor.test.sql.ConnPropInfo;
import baor.test.sql.ConnUtil;

public class SelectAllDemo implements CRUDDemo {

	@Override
	public ResultSet selectAllDemo(Statement stmt, String SQLString)
			throws SQLException {
		// TODO Auto-generated method stub
		// 创建数据集对象
		ResultSet rs = stmt.executeQuery(SQLString);
		// System.out.println("TestNo" + "\t" + "Description" + "\t" +
		// "Author");
		return rs;
	}

	@Override
	public Statement setSTMT(ConnUtil connUtil, ConnPropInfo demo)
			throws SQLException {
		// TODO Auto-generated method stub
		// 创建stmt对象
		Statement stmt = connUtil.getConnection(demo).createStatement();
		return stmt;
	}

}



package baor.test.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnUtil {
	Connection connection = null;

	public Connection getConnection(ConnPropInfo prop) {
		if (connection == null) {
			try {
				// 调用Class.forName()方法加载驱动程序
				Class.forName(prop.getDriver());
				System.out.println("成功加载MySQL驱动!");
				connection = DriverManager.getConnection(prop.getUrl(),
						prop.getUser(), prop.getPwd());
				System.out.print("成功连接到数据库!");
			} catch (ClassNotFoundException e1) {
				System.out.println("找不到MySQL驱动!");
				e1.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return connection;

	}

	public void closeConnection(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
				conn = null;
				System.out.println("连接关闭!!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


package baor.src.dbunit;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;

public class DataUtil {

	public void simgleTableExport(Connection conn, String tableName,
			String xmlFile) {
		IDatabaseConnection connection = null;
		try {
			connection = new DatabaseConnection(conn);
			QueryDataSet dataSet = new QueryDataSet(connection);
			dataSet.addTable(tableName);
			FlatXmlDataSet.write(dataSet, new FileOutputStream(xmlFile));
		} catch (DatabaseUnitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void allTableExport(Connection conn, String fileName) {
		IDatabaseConnection connection = null;
		try {
			connection = new DatabaseConne