Angus Tse
Angus Tse

Reputation: 11

[Scala][Mokito] How to stub a Future function to throw exception

I m writing test case for stubbing a repository Future function throws an Exception to simulate some DB error.

I expect the DB exception throw by Repository.create and it will be handled by Actor .recover{}. But it throws an exception and cannot be caught by .recover

// test 
it should "return NOT created message if exception thrown" in {
    val repository = mock[Repository]
    val service = TestActorRef(props(repository))
    implicit val ec: ExecutionContext = service.dispatcher
    val mockTeam = mock[Team]

    when(repository.create(any[Team])(same(ec))).thenReturn(Future.failed(throw new Exception))
    service ! CreateTeam(mockTeam)
    expectMsg(TeamNotCreated())
}


// service == actor
override def receive = {
    case CreateTeam(team) => createTeam(team)
    .recover {
      case error: Exception => TeamNotCreated()
    }.pipeTo(sender())
}
private def createTeam(team: Team)
  : Future[TeamCreateEvent] = {
        for {
          newTeam <- repository.create(team = team)
        } yield {
          if (newTeam isDefined) TeamCreated(newTeam)
          else TeamNotCreated()
        }
      }

// repository
override def create(team: TeamEntity)(implicit ec: ExecutionContext)
  : Future[Option[TeamEntity]] = {
    Future {
      val column = Team.column
      /****  I want to simulate some exception was thrown here ***/
      val newId = Team.createWithNamedValues(
        column.name -> team.name
      )
      if (newId.isValidLong) Option(team.copy(id = newId)) else None
    }
  }

Here is the output:

[info] - should return NOT created message if exception thrown *** FAILED ***
[info]   java.lang.Exception:
 should return updated message if collaborator team create success *** FAILED ***
[info]   org.mockito.exceptions.misusing.UnfinishedStubbingException: 

Unfinished stubbing detected here:
.g. thenReturn() may be missing.
[info] Examples of correct stubbing:
[info]     when(mock.isOk()).thenReturn(true);
[info]     when(mock.isOk()).thenThrow(exception);
[info]     doThrow(exception).when(mock).someVoidMethod();
[info] Hints:
[info]  1. missing thenReturn()
[info]  2. you are trying to stub a final method, you naughty developer!
[info]  3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

I had tried to to replace thenReturn(Future.failed.. with thenThrow(new Exception) but not working with error ava.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg while waiting for

Upvotes: 1

Views: 3923

Answers (1)

Shadowlands
Shadowlands

Reputation: 15074

You don't want to throw the Exception inside the Future.failed, just create it there. What happens in a call to a real repository is that an Exception is thrown in the calculation of the Future's result, and this would then be caught and placed in a Failure instance, rather than a successful calculation result being placed in a Success instance. So:

when(repository.create(any[Team])(same(ec))).thenReturn(Future.failed(new Exception(...)))

should do the job.

Upvotes: 5

Related Questions