我知道这里有关于 dbunit 的讨论。我已经阅读了其中的大部分内容,但我似乎无法找到解决问题的方法。
我已经设置了hibernate和spring。我正在做 TDD,所以我必须在编写代码之前连接一个适当的 DAO 测试框架。我想到了 Dbunit,我开始设置它。这是 ma testdataset.xml
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<table name="status">
<column>statusId</column>
<column>status</column>
<row>
<value>0</value>
<value>Available</value>
</row>
</table>
<table name="user">
<column>userId</column>
<column>firstName</column>
<column>lastName</column>
<column>username</column>
<column>password</column>
<column>email</column>
<row>
<value>0</value>
<value>system</value>
<value>admin</value>
<value>admin</value>
<value>admin</value>
<value>admin@ccbs.com</value>
</row>
</table>
<table name="reservation">
<column>reservationId</column>
<column>userId</column>
<column>reservationDate</column>
<column>startDate</column>
<column>endDate</column>
<column>statusId</column>
<row>
<value>0</value>
<value>0</value>
<value>2011-02-20 12:46:00.0</value>
<value>2011-03-01 12:00:00.0</value>
<value>2011-04-01 12:00:00.0</value>
<value>0</value>
</row>
</table>
</dataset>
一切似乎都很好,直到我尝试使用加载数据集的基类来连接一些代码。这是我的代码:
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-applicationContext.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class BaseContextSensitiveTest {
@BeforeClass
public static void setUpDatabase() throws Exception {
URL file = getInitalDatasetURL();
testDataset = createDataset(file);
}
@Before
public void init() throws Exception {
log.info("Initializing Data Set");
connection = createDBUnitConnection();
DatabaseOperation.CLEAN_INSERT.execute(connection, testDataset);
}
private static URL getInitalDatasetURL() throws FileNotFoundException {
URL file = ClassLoader.getSystemResource(TEST_DATASET_LOCATION);
if (file == null) {
throw new FileNotFoundException("Unable to find '"
+ TEST_DATASET_LOCATION + "' in the classpath");
}
return file;
}
private static IDataSet createDataset(URL file) throws IOException,
DataSetException {
return new XmlDataSet(file.openStream());
}
private IDatabaseConnection createDBUnitConnection()
throws DatabaseUnitException, SQLException {
Connection connection = getConnection();
IDatabaseConnection dbUnitConn = new DatabaseConnection(connection);
// use the hsql datatypefactory so that boolean properties work
// correctly
DatabaseConfig config = dbUnitConn.getConfig();
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new HsqldbDataTypeFactory());
return dbUnitConn;
}
一旦它命中DatabaseOperation.CLEAN_INSERT.execute(connection, testDataset);
它就会抛出以下异常:
org.dbunit.dataset.NoSuchTableException: Did not find table 'USER' in schema 'null'
at org.dbunit.database.DatabaseTableMetaData.<init>(DatabaseTableMetaData.java:142)
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:290)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at com.cottage.test.BaseContextSensitiveTest.init(BaseContextSensitiveTest.java:64)
我已经准备好所有休眠映射文件,并且数据库已经设置为没有数据。有趣的事情(或恼人的事情,取决于你如何看待它)是,如果我更改数据集中表的顺序,missingtableexception 会抱怨另一个表......用户、预订或状态。
关于我可能做错了什么的任何建议?