Last active
December 28, 2015 00:48
-
-
Save oldratlee/7415666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.dbunit.Assertion; | |
import org.dbunit.database.DatabaseConnection; | |
import org.dbunit.database.IDatabaseConnection; | |
import org.dbunit.dataset.DataSetException; | |
import org.dbunit.dataset.DefaultDataSet; | |
import org.dbunit.dataset.DefaultTable; | |
import org.dbunit.dataset.IDataSet; | |
import org.dbunit.dataset.xml.FlatXmlDataSet; | |
import org.dbunit.operation.DatabaseOperation; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.core.io.ClassPathResource; | |
import org.springframework.jdbc.datasource.DataSourceUtils; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; | |
import org.springframework.test.context.transaction.TransactionConfiguration; | |
import javax.sql.DataSource; | |
import java.sql.SQLException; | |
/* | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.4</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>2.5.6.SEC02</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.dbunit</groupId> | |
<artifactId>dbunit</artifactId> | |
<version>2.4.9</version> | |
<scope>test</scope> | |
</dependency> | |
*/ | |
/** | |
* @see <a href="http://mshijie.iteye.com/blog/475172">在Spring中结合Dbunit对Dao进行集成单元测试</a> | |
* @see <a href="http://teddywang.iteye.com/blog/652215">解决java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException异常</a> | |
*/ | |
@ContextConfiguration(locations = {"classpath:testApplicationContext.xml"}) | |
@TransactionConfiguration(defaultRollback = true) | |
public class BaseDaoTest extends AbstractTransactionalJUnit4SpringContextTests { | |
@Autowired | |
private DataSource dataSource; | |
protected IDatabaseConnection conn; | |
@Before | |
public void initDbunit() throws Exception { | |
conn = new DatabaseConnection(DataSourceUtils.getConnection(dataSource)); | |
} | |
protected static IDataSet getDataSet(String file) throws Exception { | |
return new FlatXmlDataSet(new ClassPathResource(file).getFile()); | |
} | |
/** | |
* 清空file中包含的表中的数据,并插入file中指定的数据 | |
*/ | |
protected void setUpDataSet(String file) throws Exception { | |
DatabaseOperation.CLEAN_INSERT.execute(conn, getDataSet(file)); | |
} | |
/** | |
* 验证file中包含的表中的数据和数据库中的相应表的数据是否一致 | |
*/ | |
protected void verifyDataSet(String file) throws Exception { | |
IDataSet expected = new FlatXmlDataSet(new ClassPathResource(file).getFile()); | |
IDataSet dataset = conn.createDataSet(); | |
for (String tableName : expected.getTableNames()) { | |
Assertion.assertEquals(expected.getTable(tableName), dataset.getTable(tableName)); | |
} | |
} | |
/** | |
* 清空指定的表中的数据 | |
*/ | |
protected void clearTable(String tableName) throws Exception { | |
DefaultDataSet dataset = new DefaultDataSet(); | |
dataset.addTable(new DefaultTable(tableName)); | |
DatabaseOperation.DELETE_ALL.execute(conn, dataset); | |
} | |
/** | |
* 验证指定的表为空 | |
*/ | |
protected void verifyEmpty(String tableName) throws DataSetException, SQLException { | |
Assert.assertEquals("Table " + tableName + " is not empty!", | |
0, conn.createDataSet().getTable(tableName).getRowCount()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment