Created
June 16, 2017 18:13
-
-
Save DaddyMoe/9a2fc5fec784ee54a2cf43a56cb25f2f to your computer and use it in GitHub Desktop.
Example mongoDB embedded server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.BasicDBObject; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.MongoClient; | |
import com.mongodb.client.MongoIterable; | |
import de.flapdoodle.embed.mongo.MongodExecutable; | |
import de.flapdoodle.embed.mongo.MongodStarter; | |
import de.flapdoodle.embed.mongo.config.IMongodConfig; | |
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; | |
import de.flapdoodle.embed.mongo.config.Net; | |
import de.flapdoodle.embed.mongo.distribution.Version; | |
import de.flapdoodle.embed.process.runtime.Network; | |
import lombok.extern.slf4j.Slf4j; | |
import org.hamcrest.Matchers; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.Date; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
/** | |
* Embedded MongoDB instance to be extended by Integrations Test Classes | |
* Pom dependency: | |
* <dependency> | |
* <groupId>de.flapdoodle.embed</groupId> | |
* <artifactId>de.flapdoodle.embed.mongo</artifactId> | |
* <version>2.0.0</version> | |
* </dependency> | |
* | |
* @author moses@doraventures.com | |
*/ | |
@Slf4j | |
public class EmbeddedMongoTest { | |
private static final String DATABASE_NAME = "embedded"; | |
protected MongoClient mongoClient; | |
MongodExecutable mongodExecutable = null; | |
@Before | |
public void beforeEach() throws Exception { | |
log.info("Setting up Mongo Embedded: Start"); | |
MongodStarter starter = MongodStarter.getDefaultInstance(); | |
String bindIp = "localhost"; | |
int port = 12345; | |
Net net = new Net(bindIp, port, Network.localhostIsIPv6()); | |
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.V3_4).net(net).build(); | |
mongodExecutable = starter.prepare(mongodConfig); | |
mongodExecutable.start(); | |
mongoClient = new MongoClient(bindIp, port); | |
log.info("Setting up Mongo Embedded: Completed"); | |
} | |
@After | |
public void afterEach() throws Exception { | |
MongoIterable<String> databaseNames = mongoClient.listDatabaseNames(); | |
log.info("Dropping Databases"); | |
for (String databaseName : databaseNames) { | |
log.info("Dropping Database:" + databaseName); | |
mongoClient.dropDatabase(databaseName); | |
} | |
log.info("stopping mongodExecutable"); | |
if (mongodExecutable != null) { | |
mongodExecutable.stop(); | |
} | |
log.info("Closing mongoClient"); | |
mongoClient.close(); | |
} | |
@Test | |
public void shouldCreateNewObjectInEmbeddedMongoDb() { | |
// given | |
DB db = mongoClient.getDB(DATABASE_NAME); | |
DBCollection col = db.getCollection("testCollection"); | |
// when | |
col.save(new BasicDBObject("testDoc", new Date())); | |
// then | |
assertThat(col.getCount(), Matchers.is(1L)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment