与 MongoDB 的集成测试?
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,我不知道有任何与 Mongo 的 DBUnit 直接等效的东西。 Mongo 仍然是一个新产品,因此您可能必须“自己开发”一些东西。
然而,Mongo 有几个功能可以让这一切变得简单:
基于您的数据集有很多方法可以做到这一点。但基本工具已经有了。
所以整个事情应该非常简单。尽管您必须编写大量粘合代码。
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:
Based on your dataset there are lots of ways to do this. But the basic tools are there.
So the whole thing should be pretty straightforward. Though you will have to write much of the glue code.
我所做的就是:连接到一个已知的(通常是共享的)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.
这个问题已在这里得到解答,并允许在每次测试之间启动和停止实例:
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
我知道这个问题很老了,但也许我的答案对某人有用。
这是我最近编写的一个简单的实用程序: https://github.com/kirilldev/mongomery
非常使用 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:
To check db state:
It supports placeholders for expected files which can be useful in some situations.
您可以使用具有 MongoDB 模块的 nosql-unit
You can use nosql-unit that has a MongoDB module