No data in Mongo DB in Play framework when running in Test modeAsk Question


I have play framework application v2.2 that use Mongo Database (using play-jongo). I have some testing unit class in /test folder. However if I run the unit class using test command or run directly from eclipse, there is no data found at all from Mongo DB, but if I run the application normally, I can see the data. I have used Helpers.faceApplication() method but still no data at all.

Here is the unit test code:

public void test1()  {
    Helpers.running(Helpers.fakeApplication(), new Runnable() {

        public void run() {

            //MyUser is the mongo entity
            MyUser myUser = MyUser.findById("123");
            if (myUser != null) {
      "User ID: " +;
            } else {
      "User is NULL");  //it always get here


The myUser is always returned null if I run the test unit.

I have feeling that the Helpers.fakeApplication doesn't read the /conf/applicaton.conf so it doesn't connect to mongo db.

Anyone know how to connect the play to mongo db when running in test unit?


Just some information that might be helpful. When you create the fakeApplication you are also able to provide a database to test with:

running(fakeApplication(inMemoryDatabase("test")), new Runnable() {
        public void run() {
            // some assertions here

This will of course use the H2 in-memory-database. I am not that familiar with Mongo but with regular SQL databases I would try something like this:

final Map<String, String> postgres = new HashMap<String, String>();
postgres.put("db.default.driver", "org.postgresql.Driver");
postgres.put("db.default.url", "jdbc:postgresql://localhost/myDataBase");
postgres.put("db.default.user", "postgres");
postgres.put("db.default.password", "password");

running(fakeApplication(postgres), new Runnable() {
        public void run() {
            // some assertions here

I hope this gives you some clue on how to proceed with Mongo.


It turned out that I'm using old play-jongo version (v0.4) that doesn't support loading existing mongo database in test mode. So my solution is to update the play-jongo to v0.5 (for play v2.1.x) by modifying the play-jongo dependency in file /project/Build.scala:

val appDependencies = Seq(
    "" %% "play-jongo" % "0.5.0-jongo0.4"

For another play version, please refer to the play-jongo's

Next, in /conf/application.conf I added this line:


So this is the reason I need to update the play-jongo because the old version doesn't support playjongo.test-uri configuration.

标签: mongodb unit-testing playframework-2.0 jongo
© 2014 TuiCode, Inc.