Akka-http: how to mock ActorMaterializer in unit test

Viewed 1054

I have a class A(httpClient: HttpExt)(implicit val system: ActorSystem, materializer: ActorMaterializer) that has a method called extractInfo (res: Future[HttpResponse]): Int that takes a future response and extracts an int value from the response entity.

I want to write a unit test for this method and I have the following in my unit spec class:

class ASpec extends UnitSpec with MockFactory {
  "An A" - {
    implicit val as = mock[ActorSystem]
    implicit val mat = mock[ActorMaterializer]
    val stubHttpClient = stub[HttpExt]
    val a = new A(stubHttpClient)

    "return auth token from response" in {
      val futureResponse: Future[HttpResponse] = <code that returns a Future[HttpResponse]>
      val info: Future[Option[Int]] = A.extractInfo(futureResponse)
      val result = Await.result(info, 10.seconds)
      result shouldBe Some(<a int value>)
    }
  }
}

However, on the line mock[ActorMaterializer], I got the following compilation error:

Error:(19, 28) object creation impossible, since:
it has 3 unimplemented members.
/** As seen from <$anon: akka.stream.ActorMaterializer>, the missing . 
signatures are as follows.
*  For convenience, these are usable as stub implementations.
*/
private[package akka] def actorOf(context: akka.stream.MaterializationContext,props: akka.actor.Props): akka.actor.ActorRef = ???
private[package akka] def logger: akka.event.LoggingAdapter = ???
private[package akka] def supervisor: akka.actor.ActorRef = ???
implicit val mat = mock[ActorMaterializer]

I would love some suggestions on how to solve this problem. Thank you enormously in advance!

1 Answers

I would suggest not mocking the ActorMaterializer but instead using a default implementation for example ActorMaterializer() or the one you use for your production code. At least from your example it doesn't look that you are making assertions on the ActorMaterializer itself.

Related