Akka TestKit

Akka Streams offers integration with Akka actors via the actorRef method. It is available as a Sink constructor so we can use an actor to receive elements of the flow which are then represented as messages received by the actor. It is convenient to use TestProbe from the Akka TestKit to verify assumptions about the flow. First, we need to add a dependency to the Akka TestKit in build.sbt:

libraryDependencies += com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test

Here is an example of how the TestProbe can be employed:

"the boy flow" should {
"lookup a remote seller and communicate with it" in {
val probe = TestProbe()
val source = Manager.manager.take(1)
val sink = Sink.actorRef[Groceries](probe.ref, NotUsed)
source.via(Boy.shopFlow).runWith(sink)
probe.expectMsgType[Groceries]
}
}

We test that there will be one message coming from the flow if one message goes into it. This time we're not waiting for the future to complete but formulate our assumptions with the syntax the TestProbe supports.

By now, you should have recognized the pattern we're using. First, set up the source and/or the sink, then wait for the flow to complete, and finally verify assumptions about the output of the flow. Surely enough, the Akka team abstracted this in a special test kit provided for Akka Streams.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.79.176