From 50e9b14d0b66569506da7e1fc0cc865a26108205 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Thu, 6 Sep 2018 16:29:34 +0200 Subject: [PATCH] BehaviorTestKit: get child inbox by anonymous ActorRef --- .../actor/testkit/typed/internal/BehaviorTestKitImpl.scala | 3 +++ .../akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala | 6 ++++++ .../akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala | 5 +++++ .../testkit/typed/scaladsl/SyncTestingExampleSpec.scala | 5 +++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala index a9cd50c724..a887a3b421 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala @@ -50,6 +50,9 @@ private[akka] final class BehaviorTestKitImpl[T](_path: ActorPath, _initialBehav inbox.get } + override def childInbox[U](ref: ActorRef[U]): TestInboxImpl[U] = + childInbox(ref.path.name) + override def childTestKit[U](child: ActorRef[U]): BehaviorTestKitImpl[U] = ctx.childTestKit(child) override def selfInbox(): TestInboxImpl[T] = ctx.selfInbox diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala index 0c1e2eae15..c1c57e1f5a 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/BehaviorTestKit.scala @@ -52,6 +52,12 @@ abstract class BehaviorTestKit[T] { */ def childInbox[U](name: String): TestInbox[U] + /** + * Get the child inbox for the child with the given name, or fail if there is no child with the given name + * spawned + */ + def childInbox[U](child: ActorRef[U]): TestInbox[U] + /** * Get the [[akka.actor.typed.Behavior]] testkit for the given child [[akka.actor.typed.ActorRef]]. */ diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala index 2340715b48..a9c429bd64 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/scaladsl/BehaviorTestKit.scala @@ -53,6 +53,11 @@ trait BehaviorTestKit[T] { */ def childInbox[U](name: String): TestInbox[U] + /** + * Get the child inbox for the child ActorRef, or fail if there is no such child. + */ + def childInbox[U](child: ActorRef[U]): TestInbox[U] + /** * Get the [[akka.actor.typed.Behavior]] testkit for the given child [[akka.actor.typed.ActorRef]]. */ diff --git a/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala index 27660dbf24..e7a740ab94 100644 --- a/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/docs/akka/actor/testkit/typed/scaladsl/SyncTestingExampleSpec.scala @@ -96,8 +96,9 @@ class SyncTestingExampleSpec extends WordSpec with Matchers { //#test-child-message-anonymous val testKit = BehaviorTestKit(myBehavior) testKit.run(SayHelloToAnonymousChild) - // Anonymous actors are created as: $a $b etc - val childInbox = testKit.childInbox[String](s"$$a") + val child = testKit.expectEffectType[SpawnedAnonymous[String]] + + val childInbox = testKit.childInbox(child.ref) childInbox.expectMessage("hello stranger") //#test-child-message-anonymous }