嵌入式MongoDB在运行集成测试时

时间:2021-07-24 15:12:55

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.

由于我的Java Web应用程序项目需要大量读取过滤器/查询以及与GridFS等工具的接口,因此我很难想出以上述解决方案建议的方式使用MongoDB的合理方法。

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.

因此,我正在考虑在我的集成测试中运行MongoDB的嵌入式实例。我希望它能自动启动(对于每个测试或整个套件),为每个测试刷新数据库,最后关闭。这些测试可能在开发机器和CI服务器上运行,因此我的解决方案也需要是可移植的。

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?

任何对MongoDB有更多了解的人都可以帮助我了解这种方法的可行性,并且/或者建议任何可能帮助我入门的阅读材料吗?

I'm also open to other suggestions people might have on how I could approach this problem...

我也对人们对如何解决这个问题的其他建议持开放态度......

12 个解决方案

#1


89  

I have found Embedded MongoDB library which looks quite promising and does what you have asked for.

我发现嵌入式MongoDB库看起来非常有前途,并且可以满足您的要求。

Currently supports MongoDB versions: 1.6.5 to 3.1.6, provided the binaries are still available from the configured mirror.

目前支持MongoDB版本:1.6.5到3.1.6,前提是二进制文件仍然可以从配置的镜像中获得。

Here is short example of use, which I have just tried and it works perfectly:

这是一个简短的使用示例,我刚刚尝试过,它完美地运行:

public class EmbeddedMongoTest {
    private static final String DATABASE_NAME = "embedded";

    private MongodExecutable mongodExe;
    private MongodProcess mongod;
    private Mongo mongo;

    @Before
    public void beforeEach() throws Exception {
        MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
        mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, 12345, Network.localhostIsIPv6()));
        mongod = mongodExe.start();
        mongo = new Mongo("localhost", 12345);
    }

    @After
    public void afterEach() throws Exception {
        if (this.mongod != null) {
            this.mongod.stop();
            this.mongodExe.stop();
        }
    }

    @Test
    public void shouldCreateNewObjectInEmbeddedMongoDb() {
        // given
        DB db = mongo.getDB(DATABASE_NAME);
        DBCollection col = db.createCollection("testCollection", new BasicDBObject());

        // when
        col.save(new BasicDBObject("testDoc", new Date()));

        // then
        assertThat(col.getCount(), Matchers.is(1L));
    }
}

#2


17  

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.

有Foursquare产品Fongo。 Fongo是mongo的内存中java实现。它拦截对标准mongo-java-driver的调用,用于查找,更新,插入,删除和其他方法。主要用于轻量级单元测试,您不希望启动mongo进程。

#3


7  

If you are using sbt and specs2, I wrote the same kind of wrapper for embedmongo

如果您使用的是sbt和specs2,我为embedmongo编写了相同类型的包装器

https://github.com/athieriot/specs2-embedmongo

https://github.com/athieriot/specs2-embedmongo

#4


6  

If you're using Maven you may be interested in a plugin I've created that wraps the flapdoodle.de 'embedded mongo' API:

如果您正在使用Maven,您可能会对我创建的包含flapdoodle.de'嵌入式mongo'API的插件感兴趣:

embedmongo-maven-plugin

It provides a start goal that you can use to start any version of MongoDB you want (e.g. during pre-integration-test), and a stop goal that will stop MongoDB (e.g. during post-integration-test).

它提供了一个开始目标,您可以使用它来启动您想要的任何版本的MongoDB(例如,在预集成测试期间),以及将停止MongoDB的停止目标(例如,在集成后测试期间)。

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.

使用此插件的真正好处是不需要事先安装MongoDB。 MongoDB二进制文件被下载并存储在〜/ .embedmongo中以供将来构建。

#5


4  

with spring-boot 1.3 you can use EmbeddedMongoAutoConfiguration

使用spring-boot 1.3,您可以使用EmbeddedMongoAutoConfiguration

pom.xml

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>
 ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>${embedded-mongo.version}</version>
    </dependency>

MongoConfig

MongoConfig

@Configuration
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class })
public class MongoConfig{
}

#6


3  

You can run MongoDB in memory as of version 3.2.6. From the site:

从版本3.2.6开始,您可以在内存中运行MongoDB。从网站:

Starting in MongoDB Enterprise version 3.2.6, the in-memory storage engine is part of general availability (GA) in the 64-bit builds. Other than some metadata and diagnostic data, the in-memory storage engine does not maintain any on-disk data, including configuration data, indexes, user credentials, etc.

从MongoDB Enterprise版本3.2.6开始,内存存储引擎是64位版本中通用可用性(GA)的一部分。除了一些元数据和诊断数据之外,内存存储引擎不维护任何磁盘上的数据,包括配置数据,索引,用户凭据等。

#7


2  

If you are using maven, you can use ours http://mvnrepository.com/artifact/com.wenzani/mongodb-maven-plugin

如果您使用的是maven,可以使用我们的http://mvnrepository.com/artifact/com.wenzani/mongodb-maven-plugin

#8


2  

You can also check this project which simulate a MongoDB inside JVM memory. https://github.com/thiloplanz/jmockmongo But it is still in development.

您还可以检查此项目,该项目模拟JVM内存中的MongoDB。 https://github.com/thiloplanz/jmockmongo但它仍处于开发阶段。

#9


2  

Not [just for] for unit testings, but read this blog post if you like to run MongoDB (even a cluster) as in-memory deployment if you're using Linux.

不仅仅是单元测试,但如果您想使用Linux,那么如果您想将MongoDB(甚至是集群)作为内存部署运行,请阅读此博客文章。

http://edgystuff.tumblr.com/post/49304254688

http://edgystuff.tumblr.com/post/49304254688

Would be great to have it out of the box like RavenDB though.

如果像RavenDB一样开箱即可。

#10


1  

Similar to the embedmongo-maven-plugin mentioned here, there is also a Gradle Mongo Plugin available.

与这里提到的embedmongo-maven-plugin类似,还有一个Gradle Mongo插件可用。

Like the Maven plugin it also wraps the flapdoodle EmbeddedMongoDb api and allows you to run a managed instance of Mongo from your Gradle builds.

与Maven插件一样,它也包含了flapdoodle EmbeddedMongoDb api,并允许您从Gradle构建中运行Mongo的托管实例。

#11


0  

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.

如果您希望测试反映产品在生产中的表现,请使用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.

假实现可能与真实实现不完全相同。测试时,您应该努力做到正确。执行速度排在第二位。

#12


0  

Check this code example here: https://github.com/familysyan/embedded-mongo-integ. No installation, no dependency. It's simply a platform independent ant script that do download and setup for you. It also cleans up everything after your tests.

请在此处查看此代码示例:https://github.com/familysyan/embedded-mongo-integ。没有安装,没有依赖。它只是一个独立于平台的ant脚本,可以为您下载和设置。它还可以在测试后清理所有内容。

#1


89  

I have found Embedded MongoDB library which looks quite promising and does what you have asked for.

我发现嵌入式MongoDB库看起来非常有前途,并且可以满足您的要求。

Currently supports MongoDB versions: 1.6.5 to 3.1.6, provided the binaries are still available from the configured mirror.

目前支持MongoDB版本:1.6.5到3.1.6,前提是二进制文件仍然可以从配置的镜像中获得。

Here is short example of use, which I have just tried and it works perfectly:

这是一个简短的使用示例,我刚刚尝试过,它完美地运行:

public class EmbeddedMongoTest {
    private static final String DATABASE_NAME = "embedded";

    private MongodExecutable mongodExe;
    private MongodProcess mongod;
    private Mongo mongo;

    @Before
    public void beforeEach() throws Exception {
        MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
        mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, 12345, Network.localhostIsIPv6()));
        mongod = mongodExe.start();
        mongo = new Mongo("localhost", 12345);
    }

    @After
    public void afterEach() throws Exception {
        if (this.mongod != null) {
            this.mongod.stop();
            this.mongodExe.stop();
        }
    }

    @Test
    public void shouldCreateNewObjectInEmbeddedMongoDb() {
        // given
        DB db = mongo.getDB(DATABASE_NAME);
        DBCollection col = db.createCollection("testCollection", new BasicDBObject());

        // when
        col.save(new BasicDBObject("testDoc", new Date()));

        // then
        assertThat(col.getCount(), Matchers.is(1L));
    }
}

#2


17  

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.

有Foursquare产品Fongo。 Fongo是mongo的内存中java实现。它拦截对标准mongo-java-driver的调用,用于查找,更新,插入,删除和其他方法。主要用于轻量级单元测试,您不希望启动mongo进程。

#3


7  

If you are using sbt and specs2, I wrote the same kind of wrapper for embedmongo

如果您使用的是sbt和specs2,我为embedmongo编写了相同类型的包装器

https://github.com/athieriot/specs2-embedmongo

https://github.com/athieriot/specs2-embedmongo

#4


6  

If you're using Maven you may be interested in a plugin I've created that wraps the flapdoodle.de 'embedded mongo' API:

如果您正在使用Maven,您可能会对我创建的包含flapdoodle.de'嵌入式mongo'API的插件感兴趣:

embedmongo-maven-plugin

It provides a start goal that you can use to start any version of MongoDB you want (e.g. during pre-integration-test), and a stop goal that will stop MongoDB (e.g. during post-integration-test).

它提供了一个开始目标,您可以使用它来启动您想要的任何版本的MongoDB(例如,在预集成测试期间),以及将停止MongoDB的停止目标(例如,在集成后测试期间)。

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.

使用此插件的真正好处是不需要事先安装MongoDB。 MongoDB二进制文件被下载并存储在〜/ .embedmongo中以供将来构建。

#5


4  

with spring-boot 1.3 you can use EmbeddedMongoAutoConfiguration

使用spring-boot 1.3,您可以使用EmbeddedMongoAutoConfiguration

pom.xml

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>
 ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>${embedded-mongo.version}</version>
    </dependency>

MongoConfig

MongoConfig

@Configuration
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class })
public class MongoConfig{
}

#6


3  

You can run MongoDB in memory as of version 3.2.6. From the site:

从版本3.2.6开始,您可以在内存中运行MongoDB。从网站:

Starting in MongoDB Enterprise version 3.2.6, the in-memory storage engine is part of general availability (GA) in the 64-bit builds. Other than some metadata and diagnostic data, the in-memory storage engine does not maintain any on-disk data, including configuration data, indexes, user credentials, etc.

从MongoDB Enterprise版本3.2.6开始,内存存储引擎是64位版本中通用可用性(GA)的一部分。除了一些元数据和诊断数据之外,内存存储引擎不维护任何磁盘上的数据,包括配置数据,索引,用户凭据等。

#7


2  

If you are using maven, you can use ours http://mvnrepository.com/artifact/com.wenzani/mongodb-maven-plugin

如果您使用的是maven,可以使用我们的http://mvnrepository.com/artifact/com.wenzani/mongodb-maven-plugin

#8


2  

You can also check this project which simulate a MongoDB inside JVM memory. https://github.com/thiloplanz/jmockmongo But it is still in development.

您还可以检查此项目,该项目模拟JVM内存中的MongoDB。 https://github.com/thiloplanz/jmockmongo但它仍处于开发阶段。

#9


2  

Not [just for] for unit testings, but read this blog post if you like to run MongoDB (even a cluster) as in-memory deployment if you're using Linux.

不仅仅是单元测试,但如果您想使用Linux,那么如果您想将MongoDB(甚至是集群)作为内存部署运行,请阅读此博客文章。

http://edgystuff.tumblr.com/post/49304254688

http://edgystuff.tumblr.com/post/49304254688

Would be great to have it out of the box like RavenDB though.

如果像RavenDB一样开箱即可。

#10


1  

Similar to the embedmongo-maven-plugin mentioned here, there is also a Gradle Mongo Plugin available.

与这里提到的embedmongo-maven-plugin类似,还有一个Gradle Mongo插件可用。

Like the Maven plugin it also wraps the flapdoodle EmbeddedMongoDb api and allows you to run a managed instance of Mongo from your Gradle builds.

与Maven插件一样,它也包含了flapdoodle EmbeddedMongoDb api,并允许您从Gradle构建中运行Mongo的托管实例。

#11


0  

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.

如果您希望测试反映产品在生产中的表现,请使用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.

假实现可能与真实实现不完全相同。测试时,您应该努力做到正确。执行速度排在第二位。

#12


0  

Check this code example here: https://github.com/familysyan/embedded-mongo-integ. No installation, no dependency. It's simply a platform independent ant script that do download and setup for you. It also cleans up everything after your tests.

请在此处查看此代码示例:https://github.com/familysyan/embedded-mongo-integ。没有安装,没有依赖。它只是一个独立于平台的ant脚本,可以为您下载和设置。它还可以在测试后清理所有内容。