0

Hi I am trying to test this functionality within the controller, I need to mock "MyActor" for doing the unit test.

def populateArraylist[T](hashSet: HashSet[T]): util.ArrayList[T] = {
 val list = new util.ArrayList[T]()
 hashSet.foreach(x => list.add(x))
list
}


@ApiOperation("Get the state of a something”)
def get(ID: String, dateID: String): Action[AnyContent] = Action.async 
{
implicit request =>

(MyShardProvider.shard ? MyActor.EntityPayload(
  Id,
  MySecondActor.GetStateRequest(dateId)))
  .mapTo[GetStateResponse]
  .map(x => {
    Ok(new String(JacksonSerializer.toBytes(new GetResponse(
      x.state.identifier,
      populateArraylist(x.data.transactionList.processedKeys)
    ))))
  })

}

  • Refer the post https://stackoverflow.com/questions/34265013/how-to-mock-an-akka-actor-to-unit-test-a-class – Chaitanya May 08 '18 at 07:58

1 Answers1

0

I think what you want to do is to mock the shard actor, or else you will have to actually run cluster and sharding when the unit test executes.

Easiest way is probably to either make the MyShardProvider.shard something you inject or can override (depending on how you are doing injection in your play app) in the test case to provide the ActorRef of a TestProbe instead.

That you have MyShardProvider.shard at all looks a bit fishy though, you should never have a singleton that contains an actor system, instead you should inject instances as shown in the Play docs here: https://www.playframework.com/documentation/2.6.x/ScalaAkka

johanandren
  • 11,249
  • 1
  • 25
  • 30