org.dbunit.dataset.NoSuchTableException: Did not find table ‘xxx’ in schema ‘null’

I’ve also come across this same error and the accepted fix above did not fix my problems. However I was able to find the solution.

My setup consisted of DBUnit(2.4), EclipseLink(2.1) as my JPA provider, and Postgres as my backend database. Also, in my scenario I was not dropping and recreating the tables for each test run. My test data already existed. Bad practice I know, but it was more of a test/prototyping scenario. The code below illustrates the DBUnit configuration used to fix my problem.

54    // ctx represents a spring context
55    DataSource ds = (DataSource)ctx.getBean("myDatasourceBean");
56    Connection conn = DataSourceUtils.getConnection(ds);
57    IDatabaseConnection dbUnitConn = new DatabaseConnection(conn, "public");
58    DatabaseConfig dbCfg = dbUnitConn.getConfig();
59    dbCfg.setFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);
60    IDataSet dataSet = new FlatXmlDataSet(ClassLoader.getSYstemResourceAsStream("mydbunitdata.xml"));
61    DatabaseOperation.REFRESH.execute(dbUnitConn, dataSet);

Two things in the code above fixed my problem. First I needed to define the schema DBUnit should use. That is done on line 57 above. When the new DatabaseConnection is set, the schema(“public”) should be passed in if it is not null.

Secondly, I needed DBUnit to be case sensitive about the database table names. In my DBUnit xml file(“mydbunitdata.xml”) the table names are all lowercase like they are in the database. However, if you don’t tell DBUnit to use case sensitive table names it looks for uppercase table names which Postgres didn’t like. Therefore I needed to set the case sensitive feature in DBUnit which is done on line 59.

Leave a Comment