Yes, flows using divertTo can be tested using Akka Streams TestKit.

The following code contains a test suite for the divertLeft operator introduced in the blog post:

"divertLeft" should "pass element downstream if right" in {
// given
val testErrorSink = TestSink.probe[Either[String, String]]
val element = Right("ok")

// when
val stream =
.mapMaterializedValue(_ ⇒ TestSubscriber.probe[Either[String, String]])

// then

"divertLeft" should "divert element if left" in {
// given
val passedElements: ListBuffer[String] = ListBuffer()
val divertedElements: ListBuffer[Either[String, String]] = ListBuffer()

val successfulElement = Right("Ok")
val failedElement = Left("Failed")

val errorSink = Sink
.foreach[Either[String, String]] { elm ⇒
divertedElements += elm
.mapMaterializedValue(_ ⇒ NotUsed)

// when
val sourceUnderTest =
Source(List(failedElement, successfulElement, failedElement))
.via(Flow[String].wireTap(e ⇒ passedElements += e))

// then
divertedElements.size shouldBe 2
divertedElements should contain only failedElement

passedElements.size shouldBe 1
passedElements should contain only "Ok"

with divertLeft operator defined in the following way:

object SourceOps {
implicit class SourceEitherOps[L, R, Mat](s: Source[Either[L, R], Mat]) {
def divertLeft(to: Graph[SinkShape[Either[L, R]], Mat]): Source[R, Mat] =
s.via {
Flow[Either[L, R]]
.divertTo(to, _.isLeft)
.collect { case Right(element) ⇒ element }

Software Engineer @MOIAmobility

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store