嵌入式MongoDB在运行集成测试时 [英] Embedded MongoDB when running integration tests

查看:384
本文介绍了嵌入式MongoDB在运行集成测试时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这一个的变体。

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

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.

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

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的更多知识帮助我了解这种方法的可行性,并且/或者可能建议任何可能帮助我开始的阅读材料?

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...

推荐答案

我找到了嵌入式MongoDB 库,看起来非常有前景,可以满足您的要求。

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

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

Currently supports MongoDB versions: 1.6.5 to 3.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:

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));
    }
}

这篇关于嵌入式MongoDB在运行集成测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆