mockito-scala icon indicating copy to clipboard operation
mockito-scala copied to clipboard

scala mockito error `value thenReturn is not a member of Nothing`

Open true-eye opened this issue 3 years ago • 1 comments

  • [x] The mockito message in the stacktrace have useful information, but it didn't help
  • [x] The problematic code (if that's possible) is copied here; Note that some configuration are impossible to mock via Mockito
  • [x] Provide versions (mockito / jdk / os / any other relevant information)
  • [x] Provide a Short, Self Contained, Correct (Compilable), Example of the issue (same as any question on stackoverflow.com)
  • [x] Read the contributing guide

I am new to Mockito so this may be a simple issue but I couldn't find a solution

  val users = List(User(name = "A"))
  val userRepoMock = mock[UserRepo]

  "GET /users" should {
    "return the users" in {
      when(userRepoMock.get()).thenReturn(Future.successful(Good(users)))

When I run the test, it fails as follows

   value thenReturn is not a member of Nothing
   [error]      L55:      
   when(userRepoMock.get()).thenReturn(Future.successful(Good(users)))
   [error]      L55:   

Here is the definition of UserRepo

trait UserRepo {
  def get(): Future[List[User]]
}

object UserRepo {

  class ActorImpl @Inject()(actor: UserSyncActor.Ref) extends UserRepo {
    override def get(): Future[List[User]] = {
      implicit val timeout: Timeout = 10.seconds
      actor.ref.ask(UserSyncActor.GetUsers).mapTo[List[User]]
    }
  }
}

versions: Mockito ( mockito-core: 3.3.3, mockito-scala_2.12 ) os ( MacOS )

Could anyone can help me to fix this issue, please? Thanks in advance

true-eye avatar Sep 01 '20 15:09 true-eye

The issue is you're returning a Future[Good[List[User]]] not a Future[List[User]].

dwijnand avatar Oct 29 '21 09:10 dwijnand