java 从 dbunit 测试用例中抛出 NoSuchTableException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/563531/
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-29 12:52:10  来源:igfitidea点击:

NoSuchTableException being thrown from dbunit test cases

javaexceptiondbunit

提问by Nik Reiman

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".

我正在尝试修复一个项目上的测试套件,该项目是我从另一个程序员那里继承来的一些 Java 数据库代码。该项目本身使用 hibernate 和 MySQL 作为 DB 的东西,但为了测试用例的目的,正在使用 dbunit。我可以正确加载和初始化 hibernate 的会话工厂,但是当我尝试在 Eclipse 中运行我的测试时,我不断收到异常,即“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:

我知道所有文件都在正确的位置,并且我传递到 dbunit 的实际 XML 文件没问题(我使用的是 FlatXmlDataSet 类型)。我在数据库测试用例基类中的 setUp() 方法如下所示:

@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.

在 CLEAN_INSERT 数据库操作之后,立即抛出异常,抱怨我的 XML 文件中的最后一个表,无论这是哪个表。我已经手动和使用 Eclipse 验证了 DTD 和 XML 模式,甚至确保两个文件中的表的顺序匹配。我宁愿不在这里粘贴这些文件(因为它会涉及大量的搜索替换),但相信我,我已经查看了 dbunit 示例并确保语法匹配。

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.

编辑:我忘记提及的一件事是,当我在抛出的行上放置一个断点时,我可以查看 dataSet 的结构,并看到我的所有表以及所有测试数据实际上都在那里。所以至少那部分似乎工作正常。

@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.

@Bogdan:嗯……好主意。我将尝试使用常规 INSERT 加载数据。@sleske:也是一个很好的建议。感谢您的提示;希望这将使我走上解决此问题的正确道路。

回答by Bogdan

The DatabaseOperation.CLEAN_INSERTis essentially a combination of DatabaseOperation.DELETE_ALLand DatabaseOperation.INSERT. Since the DatabaseOperation.DELETE_ALLclears the tables in reversed order, I suspect that it does not fail when clearing the last table, but when clearing the first one.

DatabaseOperation.CLEAN_INSERT本质上是一个组合DatabaseOperation.DELETE_ALLDatabaseOperation.INSERT。由于DatabaseOperation.DELETE_ALL以相反的顺序清除表格,我怀疑清除最后一个表格时它不会失败,而是清除第一个表格时。

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.

您确定您的数据集中的表确实存在于您在测试时使用的数据库中吗?你得到的错误表明他们没有。

回答by sleske

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.

你试过调试这个吗?您可以将 DBUnit 的源代码作为一个单独的项目放入 Eclipse 工作区。然后将测试的“构建路径”配置为使用 DBUnit 项目而不是 dbunit.jar。这样您就可以调试到 DBUnit 的代码中。然后你会希望看到为什么它会抛出异常。

回答by Nik Reiman

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.

sleske 确实是对的——需要更多的信息来解决这个问题,这意味着我没有问正确的问题。尽管这里的两个建议都有帮助,但鉴于我没有提出正确的问题,我会觉得将其中一个标记为正确答案会很糟糕,所以我将在此处记录我为使其工作所做的工作。

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 actualschema 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.

基本上,问题是由其他类型的模式不匹配引起的。测试数据库的 DTD 和 XML 匹配,但 DTD 不再匹配hbm.xml 文件中列出的实际模式(即我们在生产数据库中使用的模式),并且 XML 文件中的测试数据库缺少某些列后来被标记为 NOT NULL。此外,XML 包含没有 .hbm.xml 配置文件的表,因为此代码的原始作者从未编写过将使用这些表的功能。因此,即使在 DTD 中指定了它们,但缺少相应的 HBM 映射也会导致问题。

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.

此外,我不得不根据我在这篇关于同时使用 hibernate 和 dbunit 的博文中发现的内容重写我们的一些数据库测试用例基类代码。

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. :)

最后,我需要修复我们的构建过程,以便使用“hibernate-test.cfg.xml”文件代替实际配置,然后一切正常。现在我只需要弄清楚为什么一些测试用例会抛出异常。:)