运行集成测试时嵌入 MongoDB
我的问题是这个问题的变体。
由于我的 Java Web 应用程序项目需要大量读取过滤器/查询以及与 GridFS 等工具的接口,因此我正在努力想出一种按照上述解决方案建议的方式使用 MongoDB 的明智方法。
因此,我正在考虑在集成测试的同时运行 MongoDB 的嵌入式实例。我希望它自动启动(无论是针对每个测试还是整个套件),为每个测试刷新数据库,然后关闭 在最后。这些测试可能会在开发计算机以及 CI 服务器上运行,因此我的解决方案还需要可移植。
任何对 MongoDB 有更多了解的人都可以帮助我了解这种方法的可行性,和/或建议任何可能帮助我入门的阅读材料吗?
我也愿意接受人们可能对我如何解决这个问题提出的其他建议......
My question is a variation of this one.
Since my Java Web-app project requires a lot of read filters/queries and interfaces with tools like GridFS, I'm struggling to think of a sensible way to employ MongoDB in the way the above solution suggests.
Therefore, I'm considering running an embedded instance of MongoDB alongside my integration tests. I'd like it to start up automatically (either for each test or the whole suite), flush the database for every test, and shut down at the end. These tests might be run on development machines as well as the CI server, so my solution will also need to be portable.
Can anyone with more knowledge on MongoDB help me get idea of the feasibility of this approach, and/or perhaps suggest any reading material that might help me get started?
I'm also open to other suggestions people might have on how I could approach this problem...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我发现 嵌入式 MongoDB 库看起来很有前途并且可以满足您的要求。
目前支持 MongoDB 版本:
1.6.5
到3.1.6
,前提是二进制文件仍然可以从配置的镜像中获得。这是一个简短的使用示例,我刚刚尝试过并且效果很好:
I have found Embedded MongoDB library which looks quite promising and does what you have asked for.
Currently supports MongoDB versions:
1.6.5
to3.1.6
, provided the binaries are still available from the configured mirror.Here is short example of use, which I have just tried and it works perfectly:
这是 @rozky 接受的答案的更新版本(2022 年)(Mongo 和嵌入式 MongoDB 库)。
Here's an updated (for 2022) version of the accepted answer from @rozky (a lot has been changed in both the Mongo and Embedded MongoDB libraries).
有 Foursquare 产品 Fongo。
Fongo 是 mongo 的内存中 java 实现。它拦截对标准 mongo-java-driver 的调用,以进行查找、更新、插入、删除和其他方法。主要用途是轻量级单元测试,您不想启动 mongo 进程。
There is Foursquare product Fongo.
Fongo is an in-memory java implementation of mongo. It intercepts calls to the standard mongo-java-driver for finds, updates, inserts, removes and other methods. The primary use is for lightweight unit testing where you don't want to spin up a mongo process.
如果您使用 Maven,您可能对我创建的一个插件感兴趣,该插件包装了 flapdoodle.de 'embedded mongo ' API:
embedmongo-maven-plugin
它提供了一个
start
目标,您可以用来启动您想要的任何版本的 MongoDB(例如,在pre-integration-test
期间),以及一个stop
目标,将停止 MongoDB(例如在集成后测试期间)。与其他插件相比,使用此插件的真正好处是不需要预先安装 MongoDB。 MongoDB 二进制文件被下载并存储在
~/.embedmongo
中以供将来构建。If you're using Maven you may be interested in a plugin I've created that wraps the flapdoodle.de 'embedded mongo' API:
embedmongo-maven-plugin
It provides a
start
goal that you can use to start any version of MongoDB you want (e.g. duringpre-integration-test
), and astop
goal that will stop MongoDB (e.g. duringpost-integration-test
).The real benefit of using this plugin over others is that there is no requirement for MongoDB to be installed beforehand. MongoDB binaries are downloaded and stored in
~/.embedmongo
for future builds.如果您使用 sbt 和 specs2,我为 embedmongo 编写了相同类型的包装器
https://github.com/ athieriot/specs2-embedmongo
If you are using sbt and specs2, I wrote the same kind of wrapper for embedmongo
https://github.com/athieriot/specs2-embedmongo
使用 spring-boot 1.3,您可以使用 EmbeddedMongoAutoConfiguration
pom.xml
MongoConfig
with spring-boot 1.3 you can use EmbeddedMongoAutoConfiguration
pom.xml
MongoConfig
从版本 3.2.6 开始,您可以在内存中运行 MongoDB。来自网站:
You can run MongoDB in memory as of version 3.2.6. From the site:
不仅用于单元测试,还解释了如何将内存中的 mongodb 与 REST API 一起使用。
maven依赖:
=================================================== ================================
application.properties
================= =================================================== ==========
UserRepository.java
public interface UserRepository extends MongoRepository{
}
供参考和所有java代码使用以下链接:(逐步说明)
https:// www.youtube.com/watch?v=2Tq2Q7EzhSA&t=7s
Not just for unit testing, but also explained how to use inmemory mongodb with rest api.
maven dependency:
=============================================================================
application.properties
=============================================================================
UserRepository.java
public interface UserRepository extends MongoRepository{
}
for reference and all java code use below link:(step by step explanation)
https://www.youtube.com/watch?v=2Tq2Q7EzhSA&t=7s
使用
storageEngine='ephemeralForTest'
执行mongod
时性能会更好Performances are better when executing
mongod
withstorageEngine='ephemeralForTest'
要运行嵌入式 mongodb 进行集成测试,需要以下 Maven 依赖项:
尝试使用以下为
EmbeddedMongoAutoConfiguration
截取的代码:注意:
嵌入式 mongodb 将下载到以下路径。因此,请考虑该路径是否具有适当的许可。
To run Embedded mongodb for integration test following are the maven dependency needed:
Try using below code snipped for
EmbeddedMongoAutoConfiguration
:Note:
Embedded mongodb will be downloaded to below Path. So take into consideration that path have proper permission.
在生产中,您将使用真实的数据库。
如果您希望测试反映产品在生产中的行为方式,请使用 Mongo 的真实实例。
假的实现可能与真实的实现不完全相同。测试时,应力求正确。执行速度排在第二位。
In production, you will be using a real database.
If you want your tests to reflect how your product behaves in production, use a real instance of Mongo.
A fake implementation may not behave exactly the same as a real one. When testing, you should strive for correctness. Speed of execution comes second.