setusoft / silhouette-play-react-seed

A Silhouette Seed template which shows how Silhouette can be used to create a SPA with React/Redux and Play
MIT License
68 stars 21 forks source link

Mockito SmartNullPointerExceptions #49

Closed mrwillis closed 6 years ago

mrwillis commented 6 years ago

Hi,

I recently refactored this multi-project project into just one and I'm trying to fix up the tests. This isn't exactly relevant to this repo since the code is slightly different, but I was wondering if anyone could give me a hand. One interesting test is the following:

    "send a password recovery email if a user with the given email was found" in new Context {
      new WithApplication(application) {
        authTokenService.create(user.id, 5 minutes) returns Future.successful(authToken)
        userService.retrieve(loginInfo) returns Future.successful(Some(user))

        val request = FakeRequest().withJsonBody(Json.obj("email" -> email)).withCSRFToken

        Response(
          OK,
          controller.recover(request),
          "auth.password.recover.successful",
          Messages("auth.reset.email.sent", email)
        )
        there was one(mailerClient).send(any[Email])
      }
    }

The context has a mocked AuthTokenService:

 val authTokenService = mock[AuthTokenService].smart

and it is also bound correctly:

bind[AuthTokenService].toInstance(authTokenService)

But for some reason I am getting Mockito SmartNullPointerExceptions. I'm pretty perplexed - the spec does fine smart mocking other services but it has a lot of trouble with the create method:

org.mockito.exceptions.verification.SmartNullPointerException: 
You have a NullPointerException here:
-> at controllers.PasswordController.$anonfun$recover$4(PasswordController.scala:70)
because this method call was *not* stubbed correctly:
-> at controllers.PasswordController.$anonfun$recover$4(PasswordController.scala:70)
authTokenService.create(
    BSONObjectID("5ad93db11000001000c6cd7b"),
    2 hours
);

org.mockito.exceptions.verification.SmartNullPointerException: 
You have a NullPointerException here:
-> at controllers.PasswordController.$anonfun$recover$4(PasswordController.scala:70)
because this method call was *not* stubbed correctly:
-> at controllers.PasswordController.$anonfun$recover$4(PasswordController.scala:70)
authTokenService.create(
    BSONObjectID("5ad93db11000001000c6cd7b"),
    2 hours
);

    at controllers.PasswordController.$anonfun$recover$4(PasswordController.scala:70)
    at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:304)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run$$$capture(Promise.scala:60)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
    at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:81)
    at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:91)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(ForkJoinExecutorConfigurator.scala:43)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

While the project is multi-project, there is no issue. Any ideas? :(

Thanks

mrwillis commented 6 years ago

Silly mistake. The issue was I was not specifying the parameters of the mocked service properly.

true-eye commented 4 years ago

Hi, @mrwillis I am facing the same issue. Could you let me know what your mistake was, please?