PlayFramework Scala Test - get class instance via dependency injector

I use Scala Test to test my level of service. I try my best to get a class of service in my test. My test class is below

class SmsServiceSpec extends BaseSpec with OneAppPerSuite with ScalaFutures {

implicit override lazy val app: FakeApplication = FakeApplication()

"SMS Service" must {
   "able to send SMS" in {

    val smsService =  //not sure how to get instance of class here => app.injector.getInstance[SmsService]

    whenReady(smsService.sendSms("9XXXXXXX", "This is test message")) { res =>
      res mustBe true
    }
  }
 }
}

Edited code according to @easel

class SmsServiceSpec extends BaseSpec with OneAppPerSuite with ScalaFutures {

 "SMS Service" must {
"able to send SMS" in {

  @Inject val smsService: SmsService = null //not sure how to get instance of class here => app.injector.getInstance[SmsService]

  whenReady(smsService.sendSms("98XXXXXX", "This is test message")) { res =>
    res mustBe true
  }
}
}

}

I am not sure how to get the SMS service instance in the above code.

Thank,

+4
source share
1 answer

You can use Guice to inject dependencies. Good practice for abstract services at compile time and defining the binding of service abstractions and their implementation at runtime.

For instance,

class SmsServiceImpl extends SmsService

bindings (classOf [SmsService]). To (classOf [SmsServiceImpl])

https://github.com/luongbalinh/play-mongo - , , Play 2.4.2, ReactiveMongo Guice ( ).

0

All Articles