How can I get the ControllerComponents injected in my controller tests?
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/5c9eec78-23a4-4115-9f81-9efaafea428e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CAO26FW1L2Ppt55h-nHeATD_%3Dd%2B8R46-EnP9b1_45-h5%2BoWDM4w%40mail.gmail.com.
If you are using unit tests, then you should use Helpers.stubControllerComponents():Also check out the Injecting trait, which lets you use `inject[MyController]` rather than `app.injector.instanceOf[MyController]`
On Tue, Jun 27, 2017 at 8:10 AM, Justin du coeur <jduc...@gmail.com> wrote:
How are you creating your controllers for the tests? And what sort of Dependency Injection are you using?If you're using Guice for DI (the default in the Play documentation), your test suite should be inheriting from GuiceOneAppPerSuite or one of its cousins. If you do that, then IIRC you can just call `app.injector.instanceOf[MyController]` -- that will give you a properly "Guiced" copy of the controller, with ControllerComponents automatically injected.(Caveat: I'm building a custom Application using GuiceApplicationBuilder myself. But I believe that, if you don't need any customization, it ought to just work if you inherit from one of the test Suite classes...)
On Tue, Jun 27, 2017 at 8:50 AM, Andy Czerwonka <andy.cz...@gmail.com> wrote:
How can I get the ControllerComponents injected in my controller tests?
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/5c9eec78-23a4-4115-9f81-9efaafea428e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
If you are using unit tests, then you should use Helpers.stubControllerComponents():Also check out the Injecting trait, which lets you use `inject[MyController]` rather than `app.injector.instanceOf[MyController]`
On Tue, Jun 27, 2017 at 8:10 AM, Justin du coeur <jduc...@gmail.com> wrote:
How are you creating your controllers for the tests? And what sort of Dependency Injection are you using?If you're using Guice for DI (the default in the Play documentation), your test suite should be inheriting from GuiceOneAppPerSuite or one of its cousins. If you do that, then IIRC you can just call `app.injector.instanceOf[MyController]` -- that will give you a properly "Guiced" copy of the controller, with ControllerComponents automatically injected.(Caveat: I'm building a custom Application using GuiceApplicationBuilder myself. But I believe that, if you don't need any customization, it ought to just work if you inherit from one of the test Suite classes...)
On Tue, Jun 27, 2017 at 8:50 AM, Andy Czerwonka <andy.cz...@gmail.com> wrote:
How can I get the ControllerComponents injected in my controller tests?
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/5c9eec78-23a4-4115-9f81-9efaafea428e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
val params = Map(
"tyrion.mongo.database" -> "tyrion_test",
"tyrion.test" -> true
)
val app: play.api.Application = new GuiceApplicationBuilder().configure(params).build()
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/de51af43-d71b-48a9-b32c-3381d46f1124%40googlegroups.com.
class DeviceApiSpec extends TestEnvironmentSpec with Results with BeforeAndAfterAll {
"DeviceApi#siteData()" must {
// 1. This is how I create the controller here.. versus injecting it. The params come from the TestEnvironmentSpec
val api = new DeviceApi(components, app.actorSystem, app.configuration, mailer, messenger, messenger, mongo, encryptionService)
val payload = Json.obj(
"location" -> Json.obj("lat" -> 51.235685, "lng" -> -1.309197)
)
lazy val authorizedRequest = FakeRequest(POST, "/api/device")
.withHeaders(AUTHORIZATION -> apikey)
"fail an attempt without an API key" in {
val request = FakeRequest(POST, "/api/device")
.withJsonBody(payload)
val result = call(api.siteData(), request)
status(result) mustBe UNAUTHORIZED
}
}
abstract class TestEnvironmentSpec extends PlaySpec with Injecting with Inside with GuiceOneAppPerSuite with BeforeAndAfterEach {
val params = Map(
"tyrion.mongo.database" -> "tyrion_test",
"tyrion.test" -> true
)
implicit lazy override val app: play.api.Application = new GuiceApplicationBuilder().configure(params).build()
implicit lazy val materializer: Materializer = app.materializer
implicit lazy val components = Helpers.stubControllerComponents()
implicit val mongo = inject[MongoEnvironment]
implicit val db = mongo.db
val encryptionService = inject[EncryptionService]
val mailer = inject[MockMailer]
val messenger = inject[MockMessenger]}and finallyclass DeviceApi @Inject()(components: ControllerComponents,
akka: ActorSystem,
config: Configuration,
mailer: Emailer,
@Named("sms") smsMessenger: Messenger,
@Named("voice") voiceMessenger: Messenger,
mongoEnvironment: MongoEnvironment,
es: EncryptionService) extends AuthenticationController(components, mongoEnvironment, es) { ...
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/8f5746ae-df88-44da-8847-f8af4aca9754%40googlegroups.com.