fredley
fredley

Reputation: 33881

Scala Play test can't find WSClient during dependency injection

I'm trying to DI a class into a Unit Test, using the following:

val injector = new GuiceInjectorBuilder().injector()
val secured = injector.instanceOf[Secured]

However when I try and run it, I get the following error:

[info] Exception encountered when attempting to run a suite with class name: org.scalatest.DeferredAbortedSuite *** ABORTED ***
[info]   com.google.inject.ConfigurationException: Guice configuration errors:
[info] 
[info] 1) No implementation for play.api.libs.ws.WSClient was bound.
[info]   while locating play.api.libs.ws.WSClient
[info]     for parameter 3 at com.fredley.Secured.<init>(Secured.scala:36)
[info]   while locating com.fredley.Secured

...

What am I doing wrong?

Upvotes: 0

Views: 1121

Answers (1)

fredley
fredley

Reputation: 33881

The issue was not building the injector from the application (meaning Play dependencies were not loaded). This fixed the issue:

val application = new GuiceApplicationBuilder().build
val secured = application.injector.instanceOf[Secured]

Upvotes: 2

Related Questions