views:

2013

answers:

3

I'm trying to fix the test suite on an project which I've inherited from another programmer for some java database code. The project itself is using hibernate and MySQL for the DB stuff, but for the purposes of the test cases dbunit is being used. I can correctly load and initialize hibernate's session factory, but I keep getting exceptions when I try to run my tests in Eclipse, namely "org.dbunit.dataset.NoSuchTableException: mytablename".

I know that all the files are in the right place, and that the actual XML file I'm passing into dbunit is ok (I'm using the FlatXmlDataSet type). My setUp() method in the database test case base class looks like this:

@Override
protected void setUp() throws Exception {
    super.setUp();
    IDataSet dataSet = new FlatXmlDataSet(new File(mDataFile));

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    IDatabaseConnection connection = new DatabaseConnection(session.connection());

    DatabaseConfig config = connection.getConfig();
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

    DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);

    session.getTransaction().commit();
}

Right after the CLEAN_INSERT database operation, the exception gets thrown complaining about the last table in my XML file, regardless of which table this is. I have verified the DTD and XML schema by hand and with Eclipse, even going to the point of making sure the ordering of the tables in the two files matched. I'd rather not paste those files in here (as it would involve a lot of search-replacing), but trust me that I've looked through dbunit examples and made sure that the syntax matches.

Any ideas on what could be wrong? I've been googling for hours and can't come up with anything useful.

Edit: One thing I forgot to mention, is that when I place a breakpoint at the line which throws, I can look through dataSet's structure and see that all of my tables are actually in there, along with all of the test data. So at least that part seems to be working correctly.

@Bogdan: Hrm... good idea. I'll try loading the data with a regular INSERT. @sleske: Also a good suggestion. Thanks for the hints; hopefully this will set me on the right path to fixing this problem.

+1  A: 

The DatabaseOperation.CLEAN_INSERT is essentially a combination of DatabaseOperation.DELETE_ALL and DatabaseOperation.INSERT. Since the DatabaseOperation.DELETE_ALL clears the tables in reversed order, I suspect that it does not fail when clearing the last table, but when clearing the first one.

Are you sure that the tables from your data set actually exist in the database you are using while testing? The error you are getting would suggest that they don't.

Bogdan
+1  A: 

More information is needed to solve this.

Have you tried to debug this? You can put DBUnit's source code as a separat project into your Eclipse workspace. Then configure your test's "build path" to use the DBUnit project instead of a dbunit.jar. That way you can debug into DBUnit's code. Then you'll hopefully see why it throws an exception.

sleske
Thanks -- I hadn't thought of doing this, and it ultimately helped me to solve the problem. :)
Nik Reiman
+1  A: 

sleske was indeed right -- much more information was needed to solve this problem, which means I wasn't asking the right question. Although both suggestions here were helpful, I would feel bad marking one as being the correct answer, given that I wasn't asking the correct question, so I'll instead document here what I did to get this working.

Basically, the problem was caused by other types of schema mismatches. The test DB's DTD and XML matched, but the DTD no longer matched the actual schema listed in the hbm.xml files (ie, what we are using in the production DB), and the test database in the XML file was missing certain columns which were later marked as NOT NULL. Also, the XML included tables which did not have .hbm.xml config files, as the original authors of this code never got around to writing the features which would be using these tables. So even though they were specified in the DTD, the absence of a corresponding HBM mapping caused problems.

Also, I had to rewrite some of our database test case base class code based on what I found in this blog post about using hibernate and dbunit together.

Finally, I needed to fix our build process so that a "hibernate-test.cfg.xml" file was used in place of the real configuration, and then everything worked fine. Now I just need to figure out why some of the test cases are throwing exceptions. :)

Nik Reiman