磨蹭了很久,終於開始用UnitTest。原因一是和大家一樣,不想晚上做噩夢,二是在Spring下對業務層TDD,能夠不需要Tomcat,完全擺脫對顯示層開發進度的依賴,而專註快速的開發業務層。
但是我們也只在業務層使用UnitTest,因為顯示層至今沒有什麼好的UnitTest方法,而我們的商務邏輯又嚴格封裝在業務層,Controler層只做個組裝指派的基本動作,沒必要花大力氣去測試。
在Spring下的測試很簡單,在Spring-mock.jar裡有一個AbstractDependencyInjectionSpringContextTests,負責ApplicationContext的載入和屬性的動態注入。(詳看該類的JavaDoc)
所以,只要再寫一個測試父類Extend它,用DbUnit負責測試前後的資料備份和恢複。子類就可以非常輕鬆,只需指定特定的Context檔案路徑, 通過成員變數配置以下資料備份和載入的策略,還有把需要注入的成員變數設為protected就可以了.
父類的原始碼:
package com.itorgan.xxxx;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseSequenceFilter;
import org.dbunit.dataset.xml.XmlDataSet;
import org.dbunit.dataset.FilteredDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.ITableFilter;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import com.itorgan.util.DbUtil;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;
import java.util.ArrayList;
public class DAOTestCase extends AbstractDependencyInjectionSpringContextTests
{
protected ArrayList<String> contexts = null;
//DBUnit相關屬性
protected DatabaseConnection dbunitConn = null;
/**
* 是否需要測試前備份
*/
protected boolean needBackup = false;
/**
* 是否需要在測試後重新載入剛才備份的資料
*/
protected boolean needRestore = false;
/**
* 備份檔案的路徑,
* 如果為空白,預設為test/dbunitData/myTestData.xml/
*/
protected String xmlFilePath = null;
/**
* 需要備份的資料庫Table名
*/
protected String[] tableNames = null;
public DAOTestCase()
{
//設了這個,就能autowire by name,否則by setter.
setPopulateProtectedVariables(true);
contexts = new ArrayList<String>();
contexts.add("/applicationContext-Hibernate.xml");
contexts.add("/applicationContext-SetupService.xml");
}
public String[] getConfigLocations()
{
String[] tmp = new String[contexts.size()];
return contexts.toArray(tmp);
}
public void onSetUp() throws Exception
{
if (needBackup)
{
DbUtil dbUtil = new DbUtil(applicationContext);
dbunitConn = new DatabaseConnection(dbUtil.getConn(), "LH");
if (xmlFilePath == null)
xmlFilePath = "test/dbunitData/myTestData.xml";
if (tableNames != null)
{
ITableFilter filter = new DatabaseSequenceFilter(dbunitConn, tableNames);
IDataSet dataset = new FilteredDataSet(filter, dbunitConn.createDataSet());
File f_file = new File(xmlFilePath);
new File(f_file.getParent()).mkdirs();
XmlDataSet.write(dataset, new FileOutputStream(f_file));
}
else
{
throw new Exception("your choice backup data but no table names asign");
}
}
}
public void onTearDown() throws Exception
{
if (needRestore)
{
if (dbunitConn == null)
{
DbUtil dbUtil = new DbUtil(applicationContext);
dbunitConn = new DatabaseConnection(dbUtil.getConn(), "LH");
}
IDataSet dataSet = new XmlDataSet(new FileInputStream(xmlFilePath));
DatabaseOperation.REFRESH.execute(dbunitConn, dataSet);
}
dbunitConn.close();
}
}
子類的代碼
package com.itorgan.xxx;
import com.itorgan.lherp.dao.CarDAO;
import junit.framework.Test;
import junit.framework.TestSuite;
public class CarDAOImplTest extends DAOTestCase
{
protected CarDAO carDAO; //聲明為protected並且和Context,就會被動態注入
public void onSetUp() throws Exception
{
}
public void testGetGoodses() throws Exception
{
carDAO.getValidCars();
}
public static Test suite()
{
return new TestSuite(CarDAOImplTest.class);
}
}
作者Blog:http://blog.csdn.net/calvinxiu/