0

I want to do an integration test on my project which store some data into MongoDB in test mode. I am using org.Mongo .

I have some testing unit class in /test folder. However when I run the unit class from eclipse, there is no data found at all from Mongo DB, nor can I get any collection instance from it.

Here is the test code like:

@Test
public void test(){
    running(testServer(3333, fakeApplication(conf.asMap())), HTMLUNIT, new Callback<TestBrowser>() {
            public void invoke(TestBrowser browser) {
                String body = "\"name:\": \"abc\"";
                MongoStore.store(body);
            }
    });
}

Conf is different config file other than application.conf, which I use to provide staging config. MongoStore is just a wrapper to store data in mongoDB.

I can never store the date into Mongo ( I am pretty sure the rest of mongo connection is good to use ), Nor can I get an instance of DBCollection.

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

Peng
  • 1
  • 2

2 Answers2

0

I figured it out finally.

In the original code , I write a plugin to connect to mongoDB. It seems that when in test mode, play do not automatically load that plugin. So I need to add it as an additionalPlugin as a parameter of fakeApplication().

Everything works fine after that .

Inspired by a question in stackoverflow mentioning that plugins are not enabled automatically in test mode, forget the link thought( What a shame !)

other links : name of the plugins should be the same as ones in conf/play.plugins How to load different plugins in Play framework unit tests?

Play 2.0 FakeApplication setup with test configuration

Community
  • 1
  • 1
Peng
  • 1
  • 2
0

This answer might be too late and irrelevant now, but I am adding just in case someone find it useful. I wrote an opinionated approach to integration/unit testing play application that uses mongodb here. The repository uses reactivemongo as the driver, and scalatest as the testing framework.

jespeno
  • 159
  • 1
  • 11