injection error when running test with sbt

309 Views Asked by At

In a play 2.6.7 scala project with reactivemongo, I am having an issue.

When I run a test case through IntelliJ idea (CE)'s test runner, it runs without problems. But when I try to run the same test case from command line using sbt it fails. This means I have to run tests manually, which is a burden.

When the tests are run on sbt, the following errors are being received:

[info] controllers.SomeControllerSpec *** ABORTED ***
[info]   com.google.inject.ProvisionException: Unable to provision, see the following errors:
[info] 
[info] 1) No implementation for play.modules.reactivemongo.ReactiveMongoApi annotated with @play.modules.reactivemongo.NamedDatabase(value=somedatabase) was bound.
[info]   while locating play.modules.reactivemongo.ReactiveMongoApi annotated with @play.modules.reactivemongo.NamedDatabase(value=somedatabase)

and 72 of the same message gets repeated. Now the question is,

i. Why does it work on IDEA?

ii. What can I do to make it work through sbt?

(I used name dependency stuff already but did not help. Anyway, the test does work on idea already!!)

Thanks.

UPDATE Here is how the test case looks like:

package controllers

import models.Some.SomeRequest
import org.scalatestplus.play._
import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.libs.ws.WSResponse
import play.api.mvc._
import play.api.test.FakeRequest
import play.api.test.Helpers.contentAsJson

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

class SomeControllerTest extends PlaySpec with GuiceOneAppPerSuite{

  implicit val timeout: akka.util.Timeout = 5.seconds
  val controller = app.injector.instanceOf(classOf[SomeController])

  "Some test" should {
    "be successful for valid request" in {
      val someValidReq:JsValue = Json.toJson(
        SomeRequest(param1 = "value1",
          param2 = "value2")
      ).as[JsValue]

      val result: Future[Result] = controller.someMethod.apply(FakeRequest("POST", "/endpoint")
        .withJsonBody(someValidReq))
      println("test req: "+someValidReq)
      val respJson = contentAsJson(result)

      respJson.as[JsObject].value("msg").as[String] mustBe ("Successfully completed")

    }
  }
}

I can run the above test through the Run menu of IntelliJ IDEA.

However, I can't run the test by using sbt, as the following fails:

sbt testOnly controllers.SomeControllerTest 
0

There are 0 best solutions below