Searching...
Wednesday, 19 September 2012

Java Mongodb Junit test with Embedded Mongodb

00:53
https://github.com/michaelmosmann/embedmongo.flapdoodle.de This is a library for running MongoDB embedded in Java. Normally this is using only for testing purpose and no need to start and stop real mongodb instances.Below example shows how Embedded mongo is working.
Create a Maven project and add below dependency in pom.xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>de.flapdoodle.embedmongo</groupId>
<artifactId>de.flapdoodle.embedmongo</artifactId>
<version>1.11</version>
</dependency>
Test class
package com.vinod.mongo.crud;

import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import de.flapdoodle.embedmongo.MongoDBRuntime;
import de.flapdoodle.embedmongo.MongodExecutable;
import de.flapdoodle.embedmongo.MongodProcess;
import de.flapdoodle.embedmongo.config.MongodConfig;
import de.flapdoodle.embedmongo.distribution.Version;
import de.flapdoodle.embedmongo.runtime.Network;

/**
 *@authorvinodkariyathungalkumaran
 *
 */
public class EmbededMongoExample {

private static final String DB_NAME = "vinod_test";
private MongodExecutable mongodExe;
private MongodProcess mongod;
private Mongo mongo;
private DB db;
private DBCollection collection;

@Before
public void setup() throws Exception {
// Creating Mongodbruntime instance
MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
// Creating MongodbExecutable
mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345, Network.localhostIsIPv6()));
// Starting Mongodb
mongod = mongodExe.start();
mongo = new Mongo("localhost", 12345);
// Creating DB
db = mongo.getDB(DB_NAME);
// Creating collection Object and adding values
collection = db.getCollection("EmbeddedCol");
}

@After
public void teardown() throws Exception {
mongod.stop();
mongodExe.cleanup();
}

@Test
public void testAddressPersistance() {
BasicDBObject contact = new BasicDBObject();
contact.put("name", "vinod");
contact.put("address", "bangalore");
// Inserting document
collection.insert(contact);
DBCursor cursorDoc = collection.find();
BasicDBObject contact1 = new BasicDBObject();
while (cursorDoc.hasNext()) {
contact1 = (BasicDBObject) cursorDoc.next();
System.out.println(contact1);
}
assertEquals(contact1.get("name"), "vinod");
}
}
Output
Fri Jan  8 00:52:53 [conn2] insert vinod_test.EmbeddedCol 60ms
Fri Jan 8 00:52:53 [conn2] query vinod_test.EmbeddedCol nreturned:1 reslen:81 0ms
{ "_id" : { "$oid" : "568f78e511b1063f2f2a8b28"} , "name" : "vinod" , "address" : "bangalore"}

0 comments: