1

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 }
djeikyb
  • 4,470
  • 3
  • 35
  • 42

3 Answers3

2

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.

Rangi Lin
  • 9,303
  • 6
  • 45
  • 71
1

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!

Community
  • 1
  • 1
Jis Ben
  • 155
  • 1
  • 7
  • The error message is the same, but the circumstances are different. I can have four copies of `testTableNoChanges()` and only `testDbNoChanges()` fails. I'm not convinced the problem is with the CLEAN.INSERT. – djeikyb May 17 '12 at 20:09
  • Having multiple copies of `testTableNoChanges()` all pass also leads me to believe that dbunit is respecting the case of my tables. I'd guess it's in all caps for emphasis. That said, I'd be willing to try the case-sensitive option in dbunit if you can point me to it. – djeikyb May 17 '12 at 20:11
  • You would want to do something like: > DatabaseConfig config = connection.getConfig(); > config.setFeature(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE); – Jis Ben May 17 '12 at 20:30
  • Cool. setFeature() is deprecated, so I used `config.setProperty("http://www.dbunit.org/features/caseSensitiveTableNames", true);` I also found a handy list of settable properties: http://dbunit.sourceforge.net/properties.html – djeikyb May 17 '12 at 20:58
  • Unfortunately, the error remains. I checked my setProperty() with getProperty(), so it seems like dbunit ought to be case sensitive now. The error still shows an all caps 'EVENTS' – djeikyb May 17 '12 at 20:59
0

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.