NullPointerException when using FakeEnvironment


#1

I am using Silhouette inside Play 2.6 and I’m trying to write tests using ScalaTest. I created the following fakeApplication using GuiceApplicationBuilder:

22  /**
23    * The login info for the user.
24    */
25  def loginInfo() = LoginInfo("google", "test@testuser.com")
26
27  /**
28    * The fake environment.
29    */
30  def fakeUser(): User = User(0, UUID.randomUUID, "test@testuser.com", Some("test"), Some("user"))
31  implicit def fakeEnv() = FakeEnvironment[DefaultEnv](Seq(loginInfo() -> fakeUser()))
32
33
34  /**
35    * The silhouette module used to instantiate the application.
36    */
37  def silhouetteModule(fakeEnv: FakeEnvironment[DefaultEnv]): ScalaModule = new ScalaModule {
38    def configure(): Unit = {
39      bind[Environment[DefaultEnv]].toInstance(fakeEnv)
40      bind[Silhouette[DefaultEnv]].to[SilhouetteProvider[DefaultEnv]]
41    }
42  }
43
44  /**
45    * The application builder.
46    */
47  def applicationBuilder(fakeEnv: FakeEnvironment[DefaultEnv]): GuiceApplicationBuilder =
48    new GuiceApplicationBuilder().overrides(silhouetteModule(fakeEnv))
49
50  override def fakeApplication() = applicationBuilder(fakeEnv).build()

However, whenever, I run my tests, take this one for example:

  "must pass testApi with fake authenticator" in {
       System.out.println(s"Env: $env, LoginInfo: $loginInfo")
       val req = FakeRequest(GET, "/api/v1/testApi").withAuthenticator(loginInfo)
       route(app, req).map { result =>
         status(result) mustBe OK
         contentAsString(result) mustBe "test-passed"
       }.getOrElse(fail())
     }

I get a null pointer exception. :

 [info] - must must pass testApi with fake authenticator *** FAILED ***
 [info]   java.lang.NullPointerException:
 [info]   at scala.concurrent.impl.Promise$DefaultPromise.onComplete(Promise.scala:303)
 [info]   at scala.concurrent.impl.Promise.transformWith(Promise.scala:36)
 [info]   at scala.concurrent.impl.Promise.transformWith$(Promise.scala:34)
 [info]   at scala.concurrent.impl.Promise$DefaultPromise.transformWith(Promise.scala:183)
 [info]   at scala.concurrent.Future.flatMap(Future.scala:303)
 [info]   at scala.concurrent.Future.flatMap$(Future.scala:303)
 [info]   at scala.concurrent.impl.Promise$DefaultPromise.flatMap(Promise.scala:183)
 [info]   at com.mohiva.play.silhouette.api.RequestHandlerBuilder.handleAuthentication(RequestHandler.scala:138)
 [info]   at com.mohiva.play.silhouette.api.RequestHandlerBuilder.handleAuthentication$(RequestHandler.scala:137)
 [info]   at com.mohiva.play.silhouette.api.actions.SecuredRequestHandlerBuilder.handleAuthentication(SecuredAction.scala:52)
 [info]   ...

I have verified that all the data (loginInfo, User etc. are correctly retrieved from the fake auth and identity services).

Any guidance will be appreciated.

Thanks!