Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

MongoDB Rules

This is the maven companion of a blog post about mongo db integration tests.

http://eric.lemerdy.free.fr/dotclear/index.php?post/2012/03/11/Start-and-stop-mongodb-with-Junit-in-java

Florent Ramière (@framiere wanted to play with junit @rule and see if you could do a rule for a class.

Turns out it's possible, and pretty straightforward using @ClassRule

Example

Using rules you end up with the following kind of code:

publicclassWithMongoServerRuleBuilderAndClientServerRuleTest {
@ClassRulepublicstaticMongoServerRulemongoServer = newMongoServerRule();
@RulepublicMongoClientRulemongo = newMongoClientRule("test");
@Testpublicvoidshould_connect_to_the_test_database() throwsException {
assertThat(mongo.getDatabase().getCollectionNames()).isNotNull();
}
}

Plus the ClassRule and Rule code, this is strictly equivalent to the following classic BeforeClass and AfterClass code:

publicclassWithoutRulesTest {
@BeforeClasspublicstaticvoidstart_database_as_a_forked_process() throwsIOException, InterruptedException {
FiledbPath = ensureDbPathDoesNotExits();
assertThat(dbPath.mkdir()).isTrue();
List<String> lines = startMongoDBAsADaemon();
assertThat(lines.get(0)).startsWith("forked process: ");
assertThat(lines.get(1)).startsWith("all output going to: ").endsWith("logpath");
assertThat(lines).hasSize(2);
assertThatConnectionToMongodbIsPossible();
}
privatestaticList<String> startMongoDBAsADaemon() throwsIOException, InterruptedException {
ProcessBuilderprocessBuilder = newProcessBuilder("../../mongodb-osx-x86_64-2.0.3/bin/mongod", "--dbpath",
"dbpath", "--fork", "--logpath", "logpath");
processBuilder.directory(newFile("target"));
processBuilder.redirectErrorStream(true);
Processpwd = processBuilder.start();
BufferedReaderoutputReader = newBufferedReader(newInputStreamReader(pwd.getInputStream()));
Stringoutput;
List<String> lines = newArrayList<String>();
while ((output = outputReader.readLine()) != null) {
lines.add(output.toString());
}
pwd.waitFor();
assertThat(pwd.exitValue()).isEqualTo(0);
returnlines;
}
privatestaticvoidassertThatConnectionToMongodbIsPossible() throwsInterruptedException, UnknownHostException {
Mongoserver = null;
try {
while (server == null) {
Thread.sleep(250);
server = newMongoURI("mongodb://127.0.0.1").connect();
}
assertThat(server.getDatabaseNames()).hasSize(1);
} finally {
server.close();
}
}
privatestaticFileensureDbPathDoesNotExits() throwsIOException {
FiledbPath = newFile("target/dbpath");
if (dbPath.exists()) {
Files.delete(dbPath);
assertThat(dbPath.exists()).isFalse();
}
returndbPath;
}
@Testpublicvoidshould_connect_to_the_test_database() throwsException {
Mongomongo = newMongo();
try {
DBtest = mongo.getDB("test");
assertThat(test.getCollectionNames()).isNotNull();
} finally {
mongo.close();
}
}
@AfterClasspublicstaticvoidshutdown_mongodb() throwsIOException {
Mongomongo = newMongo();
try {
DBdb = mongo.getDB("admin");
CommandResultshutdownResult = db.command(newBasicDBObject("shutdown", 1));
shutdownResult.throwOnError();
fail("Expecting to loose mongodb connection on shutdown.");
} catch (Throwablee) {
assertThat(e.getMessage()).isEqualTo("can't call something : /127.0.0.1:27017/admin");
} finally {
mongo.close();
ensureDbPathDoesNotExits();
}
}
}

Builder

Just for the fun of it, Florent added a little builder to setup your mongodb install

publicclassWithMongoServerRuleAndClientServerRuleTest {
@ClassRulepublicstaticMongoServerRulemongoServer = newMongoServerRule() //
.mongodPath("c:\\dev\\mongodb\\bin\\mongodb.exe") //
.targetPath("target/mongo-temp") //
.dbRelativePath("db") //
.logRelativePath("log") //
.build();
@RulepublicMongoClientRulemongo = newMongoClientRule("test");
@Testpublicvoidshould_connect_to_the_test_database() throwsException {
assertThat(mongo.getDatabase().getCollectionNames()).isNotNull();
}
}

About

Some tests in java

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages