ReactiveMongo ConnectionNotInitialized In Test After Migrating to Play 2.5

525 views Asked by At

After migrating my Play (Scala) app to 2.5.3, some tests of my code using ReactiveMongo that once passed now fail in the setup.

Here is my code using ScalaTest:

def fixture(testMethod: (...) => Any) {
      implicit val injector = new ScaldiApplicationBuilder()
        .prependModule(new ReactiveMongoModule)
        .prependModule(new TestModule)
        .buildInj()          
      def reactiveMongoApi = inject[ReactiveMongoApi]
      def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)
      lazy val id = BSONObjectID.generate
      //Error occurs at next line
      Await.result(collection.insert(Person(id = id, slug = "test-slug", firstName = "Mickey", lastName = "Mouse")), 10.seconds)
...
}

At the insert line, I get this:

reactivemongo.core.errors.ConnectionNotInitialized: MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.']

I have tried a bunch of things like initializing collection with a lazy val instead of def. But nothing has worked.

Any insight into how to get my tests passing again is appreciated.

1

There are 1 answers

1
Vidya On BEST ANSWER

With thanks to @cchantep, the test runs as expected by replacing this code above:

def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)

with this code

def collection: BSONCollection = Await.result(reactiveMongoApi.database.map(_.collection[BSONCollection](testCollection)), 10.seconds)

In other words, reactiveMongoApi.database (along with the appropriate changes because of the Future) is the way to go.