akka Actor unit testing using testkit

There are many examples of using akka-testkit when the Actor being tested is responding to an ask:

//below code was copied from example link
val actorRef = TestActorRef(new MyActor)
// hypothetical message stimulating a '42' answer
val future = actorRef ? Say42
val Success(result: Int) = future.value.get
result must be(42)

But I have an Actor that does not respond to a sender; it instead sends a message to a separate actor. A simplified example being:

class PassThroughActor(sink : ActorRef) {
  def receive : Receive = {
    case _ => sink ! 42
  }
}

TestKit has a suite of expectMsg methods but I cannot find any examples of creating a test sink Actor that could expect a message within a unit test.

Is it possible to test my PassThroughActor?

Thank you in advance for your consideration and response.

Upvotes: 0

Views: 330

Answers (1)

Akos Krivachy
Akos Krivachy

Reputation: 4966

As mentioned in the comments you can use a TestProbe to solve this:

val sink = TestProbe()
val actorRef = TestActorRef(Props(new PassThroughActor(sink.ref)))

actorRef ! "message"

sink.expectMsg(42)

Upvotes: 2

Related Questions