Initialize embedded mongodb collection in unit tests.

208 views
Skip to first unread message

József Pollák

unread,
Jan 9, 2016, 10:32:42 AM1/9/16
to vert.x

Hello everyone,

I am writing tests for a vertx project of mine and using embedded mongodb for it. (de.flapdoodle.embed.mongo)
I was kind of wondering about the correct way to initialize my database with data.
This is what I came up with. Is it a good way to do it or are there any other preferred ways?

    @BeforeClass
    public static void init(TestContext context) throws IOException {
        /*
          ...
          Code for starting embedded mongo based on http://vertx.io/blog/combine-vert-x-and-mongo-to-build-a-giant/
          ...
        */
        MONGO = mongodExecutable.start();


        List<Item> items = getData();
        Async async = context.async(items.size());

        JsonObject config = new JsonObject().put("host", "localhost").put("port", MONGO_PORT);
        MongoClient mongoClient = MongoClient.createShared(vertx, config);
        

        for (Item item : items) {
            mongoClient.insert(
                    COLLECTION,
                    new JsonObject(Json.encode(item)),
                    res -> async.countDown());
        }
        async.await();
        mongoClient.close();
    }


Reply all
Reply to author
Forward
0 new messages