I know that discussions were discussed here. I have read most of them, but I cannot find a solution to my problem.
I set hibernation and spring. I am doing TDD, so before writing the code I had to plug in the proper DAO testing framework. Dbunit came to mind, and I got to it. Here is 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>
Everything seems good until I try to connect some code using a base class that loads a data set. Here is my code:
@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);
As soon as it gets into DatabaseOperation.CLEAN_INSERT.execute(connection, testDataset); , it throws the following exception:
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)
I have all the hibernate mapping files in place and the database is already configured without data. The funny thing (or an annoying thing depending on how you look at it) is that if I change the order of the tables in the data set, the missing exception complains about another table ... user, reservation or status.
Any suggestions on what I might be doing wrong?
user626607
source share