1

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:

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

        @Override
        public void run() {

            //MyUser is the mongo entity
            MyUser myUser = MyUser.findById("123");
            if (myUser != null) {
                Logger.info("User ID: " + myUser.id);
            } else {
                Logger.info("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?

null
  • 8,669
  • 16
  • 68
  • 98

2 Answers2

0

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.

Anton Sarov
  • 3,712
  • 3
  • 31
  • 48
  • Hi, I have tried that way, it doesn't work. I use play-jongo, it doesn't follow the db.default.* configuration. Btw, do you know how to find out the play version? – null Sep 24 '14 at 17:27
0

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(
    ...
    "uk.co.panaxiom" %% "play-jongo" % "0.5.0-jongo0.4"
)

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

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

playjongo.test-uri="mongodb://127.0.0.1:27017/mydb"

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

null
  • 8,669
  • 16
  • 68
  • 98