After a long time, we finally started using unittest. The first reason is that, like everyone else, I don't want to have a nightmare at night. The second reason is that I don't need tomcat in the business layer TDD in spring, so I can completely get rid of the dependency on the development progress of the display layer, focus on developing the business layer quickly.
However, we only use unittest on the business layer, because the display layer has no good unittest method, and our business logic is strictly encapsulated in the business layer. The controler layer only performs the basic action of assembling and dispatching, there is no need to make great effort to test.
The test in spring is very simple, there is a abstractdependencyinjectionspringcontexttests in the Spring-mock.jar, responsible for loading applicationcontext and dynamic attribute injection. (For more information, see javadoc for this class)
Therefore, you only need to write another extend test parent class and use dbunit to back up and restore data before and after the test. Subclass can be very easy. You only need to specify the specific context file path, configure the following data backup and loading policies through the member variables, and set the member variables to protected.
Source code of the parent class:
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 attributes
Protected databaseconnection dbunitconn = NULL;
/**
* Whether to back up data before testing
*/
Protected Boolean needbackup = false;
/**
* Do you need to reload the backup data after the test?
*/
Protected Boolean needrestore = false;
/**
* Path of the backup file,
* If it is null, the default value is test/dbunitdata/mytestdata. xml/
*/
Protected string xmlfilepath = NULL;
/**
* Name of the database table to be backed up
*/
Protected string [] tablenames = NULL;
Public daotestcase ()
{
// If this is set, autowire by name can be used; otherwise, 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();
}
}
Subclass code
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; // declared as protected and context, it will be dynamically injected
public void onSetUp() throws Exception
{
}
public void testGetGoodses() throws Exception
{
carDAO.getValidCars();
}
public static Test suite()
{
return new TestSuite(CarDAOImplTest.class);
}
}
Author's blog:Http://blog.csdn.net/calvinxiu/