We are now a github organisation. You are invited to participate. :)
Embedded MongoDB will provide a platform neutral way for running mongodb in unittests.
- dropping databases causing some pains (often you have to wait long time after each test)
- its easy, much easier as installing right version by hand
- you can change version per test
- Embed Process Util de.flapdoodle.embed.process
- In a Maven build using embedmongo-maven-plugin
- In a Clojure/Leiningen project using lein-embongo
- In a Scala/specs2 specification using specs2-embedmongo
http://stackoverflow.com/questions/6437226/embedded-mongodb-when-running-integration-tests http://www.cubeia.com/index.php/blog/archives/436
- https://github.com/thiloplanz/jmockmongo - mongodb mocking
- https://github.com/lordofthejars/nosql-unit - extended nosql unit testing
IMPORTANT NOTE: maven groupId and artifactId change
- groupId from de.flapdoodle.embedmongo to de.flapdoodle.embed
- artifactId from de.flapdoodle.embedmongo to de.flapdoodle.embed.mongo
Stable (Maven Central Repository, Released: 15.03.2013 - wait 24hrs for maven central)
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>1.29</version>
</dependency>
Snapshots (Repository http://oss.sonatype.org/content/repositories/snapshots)
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>1.30-SNAPSHOT</version>
</dependency>
When you fork or clone our branch you should always be able to build the library by running
mvn package
There is also a build.gradle file available which might sometimes be outdated but we try to keep it working. So the gradle command is
gradle build
Or if you want to use the gradle wrapper:
./gradlew build
- major api changes, speed improvement, easier configuration, need to update documentation
- mongod config refactor
- configurable startup timeout
- added windows2008 support
- dep version change
- bind_ip configuration parameter support (MongodConfig constructor)
- dep version change
- dep version change
- added 2.0.7, 2.2.0
- mongodb java driver update to 2.9.0 (test dependency)
- maven dep version range does not work as expected -> disabled
- dependency version range for de.flapdoodle.embed.process
- NPE fix with custom database directory
- custom database directory will not be deleted
- massive refactoring, some api breaks
- project split
- some relevant process.stop() improvements
- added some unit test support (thanx to trajano)
- added some logging only runtime config option
- added 2.0.7-rc1, 2.2.0-rc0
- command line post processor hock
- added version 2.0.6 and 2.1.2
- version refactoring
- you can now have a custom version, so you do not depend on a new release of this project
- added version 2.0.5 (main version 2.0 now points to it)
- changed http user agent
- customizeable mongod process output
- better loopback device detection for mongod process shutdown via command
- now we send ctrl+c on linux and osx, then send shutdown to server, then taskkill on windows (may the force be with us)
- disable journal for faster turnaround times
- noauth added
- customize artifact storage path
- detection if localhost is not loopback (command shutdown on mongod does not work for remote access)
- formated process output
- much better windows support
- changed back to send ctrl+c and then send shutdown
- mongod process management improvement (windows mongod shutdown improvement (alpha) (some trouble stopping process on windows - http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4770092))
- send shutdown to server
- send ctrl+c to mongod will perform clean shutdown (untested on windows using taskkill)
- now with 2.1.1
- NUMA support (alpha) - http://www.mongodb.org/display/DOCS/NUMA
- timeout fix on slow systems
- stability on win plattforms (hopefully)
- race condition and cleanup of mongod process
- fixed 64Bit detection - amd64
- now with main versions 1.6, 1.8, 2.0, 2.1
Versions: some older, 1.8.5, 1.9.0, 2.0.6, 2.1.2 Support for Linux, Windows and MacOSX.
int port = 12345;
MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0, port, Network.localhostIsIPv6());
MongodStarter runtime = MongodStarter.getDefaultInstance();
MongodExecutable mongodExecutable = null;
try {
mongodExecutable = runtime.prepare(mongodConfig);
MongodProcess mongod = mongodExecutable.start();
Mongo mongo = new Mongo("localhost", port);
DB db = mongo.getDB("test");
DBCollection col = db.createCollection("testCol", new BasicDBObject());
col.save(new BasicDBObject("testDoc", new Date()));
} finally {
if (mongodExecutable != null)
mongodExecutable.stop();
}
int port = 12345;
MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0, port, Network.localhostIsIPv6());
RuntimeConfig runtimeConfig = new RuntimeConfig();
runtimeConfig.setExecutableNaming(new UserTempNaming());
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
MongodExecutable mongodExecutable=null;
try {
mongodExecutable = runtime.prepare(mongodConfig);
MongodProcess mongod = mongodExecutable.start();
Mongo mongo = new Mongo("localhost", port);
DB db = mongo.getDB("test");
DBCollection col = db.createCollection("testCol", new BasicDBObject());
col.save(new BasicDBObject("testDoc", new Date()));
} finally {
if (mongodExecutable != null)
mongodExecutable.stop();
}
public abstract class AbstractMongoDBTest extends TestCase {
private MongodExecutable _mongodExe;
private MongodProcess _mongod;
private Mongo _mongo;
@Override
protected void setUp() throws Exception {
MongodStarter runtime = MongodStarter.getDefaultInstance();
_mongodExe = runtime.prepare(new MongodConfig(Version.Main.V2_0, 12345, Network.localhostIsIPv6()));
_mongod = _mongodExe.start();
super.setUp();
_mongo = new Mongo("localhost", 12345);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
_mongod.stop();
_mongodExe.stop();
}
public Mongo getMongo() {
return _mongo;
}
}
...
MongodForTestsFactory factory = null;
try {
factory = MongodForTestsFactory.with(Version.Main.V2_0);
Mongo mongo = factory.newMongo();
DB db = mongo.getDB("test-" + UUID.randomUUID());
DBCollection col = db.createCollection("testCol", new BasicDBObject());
col.save(new BasicDBObject("testDoc", new Date()));
} finally {
if (factory != null)
factory.shutdown();
}
...
...
IArtifactStoragePathNaming artifactStorePath = ...
ITempNaming executableNaming = ...
RuntimeConfig runtimeConfig = new RuntimeConfig();
runtimeConfig.getDownloadConfig().setArtifactStorePathNaming(artifactStorePath);
runtimeConfig.setExecutableNaming(executableNaming);
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
MongodExecutable mongodExe = runtime.prepare(mongodConfig);
...
...
RuntimeConfig runtimeConfig = new RuntimeConfig();
runtimeConfig.setProcessOutput(new ProcessOutput(Processors.namedConsole("[mongod>]"),
Processors.namedConsole("[MONGOD>]"), Processors.namedConsole("[console>]")));
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...
...
RuntimeConfig runtimeConfig=new RuntimeConfig();
IStreamProcessor mongodOutput = Processors.named("[mongod>]", new FileStreamProcessor(File.createTempFile("mongod", "log")));
IStreamProcessor mongodError = new FileStreamProcessor(File.createTempFile("mongod-error", "log"));
IStreamProcessor commandsOutput = Processors.namedConsole("[console>]");
runtimeConfig.setProcessOutput(new ProcessOutput(mongodOutput,
mongodError, commandsOutput));
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...
...
public class FileStreamProcessor implements IStreamProcessor {
private FileOutputStream outputStream;
public FileStreamProcessor(File file) throws FileNotFoundException {
outputStream = new FileOutputStream(file);
}
@Override
public void process(String block) {
try {
outputStream.write(block.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onProcessed() {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
...
...
Logger logger=...
RuntimeConfig runtimeConfig = new RuntimeConfig();
runtimeConfig.setProcessOutput(new ProcessOutput(Processors.logTo(logger, Level.INFO),
Processors.logTo(logger, Level.SEVERE), Processors.named("[console>]",Processors.logTo(logger, Level.FINE))));
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...
...
Logger logger=...
RuntimeConfig runtimeConfig = RuntimeConfig.getInstance(logger);
MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
...
...
int port = 12345;
MongodProcess mongod = null;
MongodConfig mongodConfig = new MongodConfig(new GenericVersion("2.0.7-rc1"), port, Network.localhostIsIPv6());
MongodStarter runtime = MongodStarter.getDefaultInstance();
try {
MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
mongod = mongodExecutable.start();
...
} finally {
if (mongod != null)
mongod.stop();
}
...
...
IVersion version=Version.V2_0_1;
// uses latest supported 2.1.x Version
version=Version.Main.V2_1;
// uses latest supported production version
version=Version.Main.PRODUCTION;
// uses latest supported development version
version=Version.Main.DEVELOPMENT;
Warning: maybe not as stable, as expected.
...
int port = Network.getFreeServerPort();
...
...
MongodProcess mongod = null;
MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0);
MongodStarter runtime = MongodStarter.getDefaultInstance();
try {
MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
mongod = mongodExecutable.start();
Mongo mongo = new Mongo(new ServerAddress(mongodConfig.net().getServerAddress(), mongodConfig.net().getPort()));
}
...
...
ICommandLinePostProcessor postProcessort=...
RuntimeConfig runtimeConfig = new RuntimeConfig();
runtimeConfig.setsetCommandLinePostProcessor(postProcessor);
...
YourKit is kindly supporting open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .NET Profiler.