dbunit 测试用例抛出 NoSuchTableException

发布于 2024-07-13 10:51:04 字数 1411 浏览 8 评论 0原文

我正在尝试修复一个项目的测试套件,该项目是我从另一个程序员那里继承的一些java数据库代码。 该项目本身使用 hibernate 和 MySQL 来处理数据库,但出于测试用例的目的,正在使用 dbunit。 我可以正确加载和初始化 hibernate 的会话工厂,但是当我尝试在 Eclipse 中运行测试时,我不断收到异常,即“org.dbunit.dataset.NoSuchTableException: mytablename”。

我知道所有文件都位于正确的位置,并且我传递到 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();
}

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

关于可能出什么问题的任何想法吗? 我已经用谷歌搜索了几个小时,但找不到任何有用的东西。

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

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

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

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:

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-07-20 10:51:04

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

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

The DatabaseOperation.CLEAN_INSERT is essentially a combination of DatabaseOperation.DELETE_ALL and DatabaseOperation.INSERT. Since the DatabaseOperation.DELETE_ALL clears the tables in reversed order, I suspect that it does not fail when clearing the last table, but when clearing the first one.

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.

つ低調成傷 2024-07-20 10:51:04

需要更多信息来解决这个问题。

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

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.

女皇必胜 2024-07-20 10:51:04

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

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

另外,我必须根据我发现的 重写一些数据库测试用例基类代码在这篇关于一起使用 hibernate 和 dbunit 的博客文章中

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

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.

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 actual schema 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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文