djeikyb
djeikyb

Reputation: 4598

dbunit test fails with NoSuchTableException, but table exists

Error

java org.junit.runner.JUnitCore TestCase
JUnit version 4.10
.E..
Time: 0.28
There was 1 failure:
1) testDbNoChanges(TestCase)
org.dbunit.dataset.NoSuchTableException: Did not find table 'EVENTS' in schema 'null'

Question

What does this error mean? What am I doing wrong? Why is the schema null?

The second test, that actually tests a table, passes. This test, which is supposed to just test the database, fails. The tables all exist before and after.

mysql> show tables;
+---------------+
| Tables_in_cal |
+---------------+
| events        |
| guests        |
| test          |
+---------------+
3 rows in set (0.00 sec)

Source

I believe this is the useful snippet, but everything is at https://bitbucket.org/djeikyb/simple_dbunit

 36 public class TestCase
 37 {
 38 
 39   private IDatabaseTester database_tester;
 40 
 41 
 42   public IDataSet getDataSet() throws FileNotFoundException, DataSetException
 43   {
 44     return new FlatXmlDataSetBuilder().build(
 45         /*
 46         new FileInputStream("src/simple_dbunit/expected_dataset.xml"));
 47         new FileInputStream("dataset.xml"));
 48          */
 49         new FileInputStream("dataset.xml"));
 50   }
 51 
 52 
 53   @Before
 54   public void setUp() throws Exception
 55   {
 56     database_tester = new JdbcDatabaseTester("com.mysql.jdbc.Driver",
 57                                             "jdbc:mysql://localhost/cal",
 58                                             "cal",
 59                                             "cal");
 60     database_tester.setDataSet(getDataSet());
 61     database_tester.onSetup();
 62   }
 63 
 64   @Test
 65   public void testDbNoChanges() throws Exception
 66   {
 67     // expected
 68     IDataSet expected_data_set = getDataSet();
 69 
 70     // actual
 71     IDatabaseConnection connection = database_tester.getConnection();
 72     IDataSet actual_data_set = connection.createDataSet();
 73 
 74     // test
 75     Assertion.assertEquals(expected_data_set, actual_data_set);
 76   }
 77 
 78   @Test
 79   public void testTableNoChanges() throws Exception
 80   {
 81     // expected
 82     ITable expected_table = getDataSet().getTable("test");
 83 
 84     // actual
 85     IDatabaseConnection connection = database_tester.getConnection();
 86     IDataSet actual_data_set = connection.createDataSet();
 87     ITable actual_table = actual_data_set.getTable("test");
 88 
 89     // test
 90     Assertion.assertEquals(expected_table, actual_table);
 91   }
 92 
 93   @Test
 94   public void testTableNoChanges1() throws Exception
 95   {
 96     // expected
 97     ITable expected_table = getDataSet().getTable("test");
 98 
 99     // actual
100     IDatabaseConnection connection = database_tester.getConnection();
101     IDataSet actual_data_set = connection.createDataSet();
102     ITable actual_table = actual_data_set.getTable("test");
103 
104     // test
105     Assertion.assertEquals(expected_table, actual_table);
106   }
107 
108 }

Upvotes: 1

Views: 13100

Answers (3)

Mykola Afanasiev
Mykola Afanasiev

Reputation: 11

Add dbunit.yml file in datasets folder. And set next property:

caseInsensitiveStrategy: !!com.github.database.rider.core.api.configuration.Orthography 'LOWERCASE'
properties:
  caseSensitiveTableNames: false

It`s disable your the case-sensitive option.

Upvotes: 0

Rangi Lin
Rangi Lin

Reputation: 9461

I don't have MySql environment at the moment, so I can not confirm it. But I think I know what is the problem.

When working with different database with DBUnit, sometimes you need to set up database specified config. Here is the example for MySql :

IDatabaseConnection dbConn = getConnection();
dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
    new MySqlDataTypeFactory());
dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, 
    new MySqlMetadataHandler());

The catalog/schema in MySql is somewhat different from other database, so if you don't use MySqlMetadataHandler, you can never find a correct table.

If you want to make JdbcDatabaseTester cooperate with MySql, You can extend it and override the getConnection() method like this :

public class MySqlDatabaseTester extends JdbcDatabaseTester {

  ...   

  @Override
  public IDatabaseConnection getConnection() throws Exception {
    IDatabaseConnection dbConn = super.getConnection();
    dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
        new MySqlDataTypeFactory());
    dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, 
        new MySqlMetadataHandler());
    return dbConn;
  }
}

or simply use MySqlConnection as IDatabaseConnection in your test case.

Upvotes: 2

Jis Ben
Jis Ben

Reputation: 155

See Here for similar problem

I'm pretty sure you also need to set the case-sensitive option. You DB backend might not like the upper case table name request!

Upvotes: 1

Related Questions