与 MongoDB 的集成测试?

发布于 2024-09-26 05:45:01 字数 140 浏览 2 评论 0原文

我需要使用 Java 对 Mongo 数据库进行多次集成测试,并且我一直在寻找类似 DbUnit 的解决方案(DbUnit 用于 Hibernate),它可以使用自定义数据填充我的数据库,并在每次运行后重置状态。

有什么建议吗?

谢谢

I need to do several integration tests on a Mongo database using Java, and I was looking for a DbUnit-like solution (DbUnit is for Hibernate) that can populate my database with custom data, and reset the state after each run.

Any tips?

Thanks

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

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

发布评论

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

评论(5

爱冒险 2024-10-03 05:45:01

首先,我不知道有任何与 Mongo 的 DBUnit 直接等效的东西。 Mongo 仍然是一个新产品,因此您可能必须“自己开发”一些东西。

然而,Mongo 有几个功能可以让这一切变得简单:

  1. 它以最小的权限运行
  2. 它可以简单地在准备好的文件上“运行”
  3. 它实际上没有模式(索引除外)
  4. 它可以使用 JSON 数据

基于您的数据集有很多方法可以做到这一点。但基本工具已经有了。

  • 您应该能够从您的测试中启动专门用于您的测试的版本。
  • 您应该能够从 JSON 文件导入“状态”数据。
  • 您应该能够从 JS 文件应用任何服务器端函数(从头开始)。

所以整个事情应该非常简单。尽管您必须编写大量粘合代码。

To start off, I don't know of any direct equivalent to DBUnit for Mongo. Mongo is still a new product, so you'll probably have to "roll your own" for some of this stuff.

However, there are several features of Mongo that should make this easy:

  1. It runs with minimal permissions
  2. It can simply "run" on prepared files
  3. It doesn't really have a schema (except for indexes)
  4. It can work of JSON data

Based on your dataset there are lots of ways to do this. But the basic tools are there.

  • You should be able to start a version specifically for your test, from your test.
  • You should be able to import "state" data from JSON file.
  • You should be able to apply any server-side functions from a JS file (from scratch).

So the whole thing should be pretty straightforward. Though you will have to write much of the glue code.

心安伴我暖 2024-10-03 05:45:01

我所做的就是:连接到一个已知的(通常是共享的)mongo 实例,但使用 UUID 为每个测试运行创建一个新的唯一数据库。您不必担心创建集合,因为它们是在您第一次在集合中存储文档时延迟创建的。在存储库或 DAO 的构造函数中创建您需要的任何索引;如果索引已经存在,mongo 索引创建将立即成功,无需执行任何工作。显然,您不需要担心架构迁移;-)

该方案要求您从空数据存储开始,但它是已知状态,因此如果需要,可以很容易地在测试的设置阶段进行填充。

测试完成后,在拆卸阶段删除整个数据库。

Here's what I do: connect to a known (often shared) mongo instance, but create a new unique database for each test run using a UUID. You don't have to worry about creating collections, as they are created lazily when you store documents in them for the first time. Create any indexes you need in the constructor of the repository or DAO; mongo index creations succeed immediately without doing any work if the index already exists. Obviously, you don't need to worry about schema migrations ;-)

This scheme requires to you to start from an empty datastore, but it's a known state, so it's easy enough to populate in the setup phase of your tests if need be.

When the test is done, delete the entire database in the teardown phase.

贱贱哒 2024-10-03 05:45:01

这个问题已在这里得到解答,并允许在每次测试之间启动和停止实例:
https://stackoverflow.com/a/9830861/82609

但是每个测试之间的启动/停止似乎会减慢集成测试的速度,因此您最好为整个测试套件启动/停止它:
https://stackoverflow.com/a/14171993/82609

This question has been answered here and permits to start and stop an instance between each test:
https://stackoverflow.com/a/9830861/82609

But start/stop between each test seems to slow down integration tests, and thus you'd better start/stop it for the whole test suite:
https://stackoverflow.com/a/14171993/82609

懒的傷心 2024-10-03 05:45:01

我知道这个问题很老了,但也许我的答案对某人有用。
这是我最近编写的一个简单的实用程序: https://github.com/kirilldev/mongomery

非常使用 json 文件中的数据填充数据库很简单:

//db here is a com.mongodb.DB instance
MongoDBTester mongoDBTester = new MongoDBTester(db);
mongoDBTester.setDBState("predefinedTestData.json");

检查数据库状态:

mongoDBTester.assertDBStateEquals("expectedTestData.json");

它支持预期文件的占位符,这在某些情况下很有用。

I know this question is old, but maybe my answer will be useful for someone.
Here is a simple util that I wrote it recently: https://github.com/kirilldev/mongomery

Very simple to populate db with data from json file:

//db here is a com.mongodb.DB instance
MongoDBTester mongoDBTester = new MongoDBTester(db);
mongoDBTester.setDBState("predefinedTestData.json");

To check db state:

mongoDBTester.assertDBStateEquals("expectedTestData.json");

It supports placeholders for expected files which can be useful in some situations.

地狱即天堂 2024-10-03 05:45:01

您可以使用具有 MongoDB 模块的 nosql-unit

You can use nosql-unit that has a MongoDB module

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