我知道这里有关于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);它将引发以下异常:
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)
我已经准备好了所有的hibernate映射文件,并且数据库已经设置为没有数据。有趣的事情(或令人讨厌的事情,取决于您的查看方式)是,如果我更改数据集中表的顺序,则missingtableexception会抱怨另一个表…用户,保留或状态。
关于我可能在做错的任何建议吗?
DBUnit不会为您创建数据库表,因为它从创建数据集xml到创建预期数据库模式的信息有限。
当与hibernate一起使用时,它将为每个pojo(您要映射到内存中的测试数据库表)都需要正确的hbm映射文件,该文件最终将在测试中使用。没有映射文件,您将获得org.dbunit.dataset.NoSuchTableException: Did not find table 'xxx' in schema 'yyy'。
org.dbunit.dataset.NoSuchTableException: Did not find table 'xxx' in schema 'yyy'
此外,还需要有效的hibernate.cfg.xml,并使用所有的hibernate映射文件正确配置。
您可以通过hibernate.hbm2ddl.auto=create-drop在属性文件中设置此属性来委派数据库创建以使其hibernate。
hibernate.hbm2ddl.auto=create-drop
该错误消息有点误导。应该可能包含更多信息,以了解丢失的hibernate映射文件的影响-但这是DBunit Wiki上的讨论。