对于 TestNG,当使用 Hibernate 查询包含复杂模型的数据源时,预填充数据源的正确方法是什么?

发布于 2024-09-27 01:19:20 字数 515 浏览 0 评论 0 原文

我希望为我们基于 Seam Framework 的网站的内部搜索引擎编写测试,该引擎使用 Hibernate + Lucene 索引(在 DB2 上)进行查询。当项目的数据模型非常复杂(考虑到实体关系和字段约束)时,在运行 TestNG 套件之前填充数据源的最佳解决方案是什么?对于某些测试用例,至少有十几个数据库表需要彼此相关的行,以便遵守数据模型的约束。理想情况下,将使用 Hypersonic,因为内存中的使用将缩短我们的构建过程的运行时间。

希望我的问题很清楚,因为如果不抛出大量描述性文本和专有代码,很难对我的问题形成完整的描述。基本上,考虑到我们已经编写的数据模型和 populate.sql 脚本,以编程方式创建每个实体(通过 Hibernate 的 Home 对象实例化所有对象、设置每个属性、持久化到数据源并在 FacesRequest @Test 中提交事务)太笨重了(并且在 DB2 上执行以在本地运行 JBoss 托管的网站)无法在 Hypersonic 上使用!我在网上或书中遇到的每个 TestNG 示例都包含极其简单的数据集,这些数据集并没有表明解决我的问题的明确方法。

I wish to write tests for our Seam Framework-based web site's internal search engine that uses Hibernate + Lucene indexing (on DB2) for queries. What is the best solution for populating the data source before the TestNG suite is run when the project's data model is quite complex considering entity relationships and field constraints? For some test cases, at least a dozen database tables would require rows relating to each other in order to adhere to the data model's constraints. Ideally Hypersonic would be used since in-memory usage will shorten our build process' running time.

Hopefully my question is clear as it's difficult to formulate a complete picture of my problem without throwing up a massive wall of descriptive text and proprietary code. Basically, creating each entity programmatically (instantiating all objects via Hibernate's Home object, setting each property, persisting to data source, and committing transaction in a FacesRequest @Test) is too unwieldy given the data model and the populate.sql script we already have written (and is executed on DB2 for running our JBoss-hosted web site locally) can't be used on Hypersonic! And every TestNG example I come across online or in books contain brutally simple data sets that don't indicate a clear approach to my problem.

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

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

发布评论

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

评论(2

亣腦蒛氧 2024-10-04 01:19:20

要创建复杂的数据,同时隐藏复杂的设置,我的建议是使用描述的构建器模式此处 它允许您创建诸如“

Person person = PersonBuilder.validPerson()
                             .withAddress(AddressBuider.validAddress())
                             .build();

关于 h2 和 db2 互操作性”之类的内容。最先进的 POJO in Action 书中说

使用内存数据库时的一个挑战是确保其架构与生产数据库的架构相同。如果 ORM 框架生成数据库模式,这不是问题。但是,如果单独维护生产数据库架构,则其定义可能与内存数据库不兼容。例如,它可以使用供应商特定的数据类型和其他功能。 为了使用内存数据库,您需要使用不同的架构定义或从 ORM 生成其架构。无论哪种情况,都无法保证内存数据库与生产数据库具有相同的架构。

虽然您的问题涉及 Hypersonic,但 H2 功能包括

  • IBM DB2 的兼容模式、Apache Derby、HSQLDB、MS SQL Server、MySQL、Oracle 和 PostgreSQL。

也许它可以解决你想要的。尝试一下。

可以使用不同的方法来填充测试所需的数据。 XUnit 夹具设置模式 解释了一个有用的指南。

其他可以帮助您的工具是

To create your complex data and, at the same time, hide complex setup, my advice is to use builder pattern which is described here It allows you to create stuff like

Person person = PersonBuilder.validPerson()
                             .withAddress(AddressBuider.validAddress())
                             .build();

About h2 and db2 interoperability. State of the art POJO in Action book says

One challenge when using an in-memory database is ensuring that its schema is identical to the production database’s schema. This isn’t a problem if the ORM framework generates the database schema. However, if the production database schema is maintained separately, then its definition might not be compatible with the in-memory database. It could, for example, use vendor-specific data types and other features. In order to use an in-memory database, you will need to use a different schema definition or generate its schema from the ORM. In either case, there is no guarantee that the in-memory database has the same schema as the production database.

Although your question draws Hypersonic, H2 features includes

  • Compatibility modes for IBM DB2, Apache Derby, HSQLDB, MS SQL Server, MySQL, Oracle, and PostgreSQL.

Maybe it can solve what you want. Try it.

Differents approachs can be used to populate the data needed to test. A useful guide is explained by XUnit Fixture Setup Patterns.

Other tools which can help you is

苏辞 2024-10-04 01:19:20

基本上,考虑到数据模型和我们已经编写的 populate.sql 脚本(并且在 DB2 上执行以在本地运行 JBoss 托管的网站),以编程方式创建每个实体(...)太笨重了用于高超音速!

如果您想重用 SQL 脚本,可以尝试使用 DB2 兼容模式的 H2,按照@Arthur的建议。如果它不起作用,请尝试 JavaDB/Derby,它与 DB2 的语言兼容,也可以在“内存中”使用。

但如果脚本很大,恐怕测试会很慢。在这种情况下,使用 DbUnit 和较小的数据集可能是一种替代方案。看看内置的 DBUnitSeamT​​est使用 Seam、DBUnit 和 TestNG 进行功能测试

如果您采用 DbUnit 方式,则可以使用 databene benerator 之类的工具来 生成模拟数据,然后将它们转储为 DbUnit 数据集(有 一个 Eclipse 插件 来简化此任务)。

Basically, creating each entity programmatically (...) is too unwieldy given the data model and the populate.sql script we already have written (and is executed on DB2 for running our JBoss-hosted web site locally) can't be used on Hypersonic!

If you want to reuse your SQL script, you could try H2 with the DB2 compatibility mode, as suggested by @Arthur. If it doesn't work, try JavaDB/Derby, which is DB2's language compatible and can also be used "in-memory".

But if the script is huge, I'm afraid tests will be pretty slow. In that case, using DbUnit and smaller dataset could be an alternative. Have a look at the built-in DBUnitSeamTest and Functional testing with Seam, DBUnit, and TestNG.

And if you go the DbUnit way, you could use a tool like databene benerator to generate mock data and then dump them as DbUnit dataset (there is an eclipse plugin to ease this task).

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