java dbunit 测试因 NoSuchTableException 而失败,但表存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10641687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:56:24  来源:igfitidea点击:

dbunit test fails with NoSuchTableException, but table exists

javajunitjunit4dbunit

提问by djeikyb

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

我相信这是有用的片段,但一切都在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 }

回答by Jis Ben

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!

我很确定您还需要设置区分大小写的选项。您的数据库后端可能不喜欢大写的表名请求!

回答by Rangi Lin

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

我目前没有MySql环境,所以无法确认。但我想我知道问题出在哪里。

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

当使用 DBUnit 处理不同的数据库时,有时您需要设置数据库指定的配置。这是 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.

MySql 中的目录/模式与其他数据库有些不同,因此如果不使用MySqlMetadataHandler,则永远找不到正确的表。

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

如果你想JdbcDatabaseTester与MySql合作,你可以扩展它并覆盖这样的getConnection()方法:

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 MySqlConnectionas IDatabaseConnectionin your test case.

或者简单地使用MySqlConnectionIDatabaseConnection在你的测试用例。

回答by Rodion Raskolnikov

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

在 datasets 文件夹中添加 dbunit.yml 文件。并设置下一个属性:

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

It`s disable your the case-sensitive option.

它禁用了区分大小写的选项。