class TestKit extends TestKitBase
Test kit for testing actors. Inheriting from this class enables reception of
replies from actors, which are queued by an internal actor and can be
examined using the expectMsg... methods. Assertions and bounds concerning
timing are available in the form of within blocks.
class Test extends TestKit(ActorSystem()) { try { val test = system.actorOf(Props[SomeActor]) within (1.second) { test ! SomeWork expectMsg(Result1) // bounded to 1 second expectMsg(Result2) // bounded to the remainder of the 1 second } } finally { system.terminate() } } finally { system.terminate() } }
Beware of two points:
- the ActorSystem passed into the constructor needs to be shutdown, otherwise thread pools and memory will be leaked
- this class is not thread-safe (only one actor with one queue, one stack
   of withinblocks); it is expected that the code is executed from a constructor as shown above, which makes this a non-issue, otherwise take care not to run tests within a single test class instance in parallel.
It should be noted that for CI servers and the like all maximum Durations are scaled using their Duration.dilated method, which uses the TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry "akka.test.timefactor".
- Source
- TestKit.scala
- Since
- 1.1 
- Alphabetic
- By Inheritance
- TestKit
- TestKitBase
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- Protected
Instance Constructors
-  new TestKit(_system: ActorSystem)
Value Members
-   final  def !=(arg0: Any): Boolean- Definition Classes
- AnyRef → Any
 
-   final  def ##: Int- Definition Classes
- AnyRef → Any
 
-  def +(other: String): String
-  def ->[B](y: B): (TestKit, B)
-   final  def ==(arg0: Any): Boolean- Definition Classes
- AnyRef → Any
 
-   final  def asInstanceOf[T0]: T0- Definition Classes
- Any
 
-    def assertForDuration[A](a: => A, max: FiniteDuration, interval: Duration = 100.millis): AEvaluate the given assert every intervaluntil exception is thrown ormaxtimeout is expired.Evaluate the given assert every intervaluntil exception is thrown ormaxtimeout is expired.Returns the result of last evaluation of the assertion. If no timeout is given, take it from the innermost enclosing withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "akka.test.timefactor". - Definition Classes
- TestKitBase
 
-    def awaitAssert[A](a: => A, max: Duration = Duration.Undefined, interval: Duration = 100.millis): AEvaluate the given assert every intervaluntil it does not throw an exception and return the result.Evaluate the given assert every intervaluntil it does not throw an exception and return the result.If the maxtimeout expires the last exception is thrown.If no timeout is given, take it from the innermost enclosing withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "akka.test.timefactor". - Definition Classes
- TestKitBase
 
-    def awaitCond(p: => Boolean, max: Duration = Duration.Undefined, interval: Duration = 100.millis, message: String = ""): UnitAwait until the given condition evaluates to trueor the timeout expires, whichever comes first.Await until the given condition evaluates to trueor the timeout expires, whichever comes first.If no timeout is given, take it from the innermost enclosing withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "akka.test.timefactor". - Definition Classes
- TestKitBase
 
-    def childActorOf(props: Props): ActorRefSpawns an actor as a child of this test actor with an auto-generated name and stopping supervisor strategy, returning the child's ActorRef. Spawns an actor as a child of this test actor with an auto-generated name and stopping supervisor strategy, returning the child's ActorRef. - props
- Props to create the child actor 
 - Definition Classes
- TestKitBase
 
-    def childActorOf(props: Props, name: String): ActorRefSpawns an actor as a child of this test actor with a stopping supervisor strategy, and returns the child's ActorRef. Spawns an actor as a child of this test actor with a stopping supervisor strategy, and returns the child's ActorRef. - props
- Props to create the child actor 
- name
- Actor name for the child actor 
 - Definition Classes
- TestKitBase
 
-    def childActorOf(props: Props, supervisorStrategy: SupervisorStrategy): ActorRefSpawns an actor as a child of this test actor with an auto-generated name, and returns the child's ActorRef. Spawns an actor as a child of this test actor with an auto-generated name, and returns the child's ActorRef. - props
- Props to create the child actor 
- supervisorStrategy
- Strategy should decide what to do with failures in the actor. 
 - Definition Classes
- TestKitBase
 
-    def childActorOf(props: Props, name: String, supervisorStrategy: SupervisorStrategy): ActorRefSpawns an actor as a child of this test actor, and returns the child's ActorRef. Spawns an actor as a child of this test actor, and returns the child's ActorRef. - props
- Props to create the child actor 
- name
- Actor name for the child actor 
- supervisorStrategy
- Strategy should decide what to do with failures in the actor. 
 - Definition Classes
- TestKitBase
 
-    def clone(): AnyRef- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @HotSpotIntrinsicCandidate() @native()
 
-  def ensuring(cond: (TestKit) => Boolean, msg: => Any): TestKit
-  def ensuring(cond: (TestKit) => Boolean): TestKit
-  def ensuring(cond: Boolean, msg: => Any): TestKit
-  def ensuring(cond: Boolean): TestKit
-   final  def eq(arg0: AnyRef): Boolean- Definition Classes
- AnyRef
 
-    def equals(arg0: AnyRef): Boolean- Definition Classes
- AnyRef → Any
 
-    def expectMsg[T](max: FiniteDuration, hint: String, obj: T): TReceive one message from the test actor and assert that it equals the given object. Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - returns
- the received object 
 - Definition Classes
- TestKitBase
 
-    def expectMsg[T](max: FiniteDuration, obj: T): TReceive one message from the test actor and assert that it equals the given object. Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - returns
- the received object 
 - Definition Classes
- TestKitBase
 
-    def expectMsg[T](obj: T): TSame as expectMsg(remainingOrDefault, obj), but correctly treating the timeFactor.Same as expectMsg(remainingOrDefault, obj), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgAllClassOf[T](max: FiniteDuration, obj: Class[_ <: T]*): Seq[T]Receive a number of messages from the test actor matching the given number of classes and assert that for each given class one is received which is of that class (equality, not conformance). Receive a number of messages from the test actor matching the given number of classes and assert that for each given class one is received which is of that class (equality, not conformance). This construct is useful when the order in which the objects are received is not fixed. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - Definition Classes
- TestKitBase
 
-    def expectMsgAllClassOf[T](obj: Class[_ <: T]*): Seq[T]Same as expectMsgAllClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor.Same as expectMsgAllClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgAllConformingOf[T](max: FiniteDuration, obj: Class[_ <: T]*): Seq[T]Receive a number of messages from the test actor matching the given number of classes and assert that for each given class one is received which conforms to that class (and vice versa). Receive a number of messages from the test actor matching the given number of classes and assert that for each given class one is received which conforms to that class (and vice versa). This construct is useful when the order in which the objects are received is not fixed. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. Beware that one object may satisfy all given class constraints, which may be counter-intuitive. - Definition Classes
- TestKitBase
 
-    def expectMsgAllConformingOf[T](obj: Class[_ <: T]*): Seq[T]Same as expectMsgAllConformingOf(remainingOrDefault, obj...), but correctly treating the timeFactor.Same as expectMsgAllConformingOf(remainingOrDefault, obj...), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgAllOf[T](max: FiniteDuration, obj: T*): Seq[T]Receive a number of messages from the test actor matching the given number of objects and assert that for each given object one is received which equals it and vice versa. Receive a number of messages from the test actor matching the given number of objects and assert that for each given object one is received which equals it and vice versa. This construct is useful when the order in which the objects are received is not fixed. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. dispatcher ! SomeWork1() dispatcher ! SomeWork2() expectMsgAllOf(1 second, Result1(), Result2()) - Definition Classes
- TestKitBase
 
-    def expectMsgAllOf[T](obj: T*): Seq[T]Same as expectMsgAllOf(remainingOrDefault, obj...), but correctly treating the timeFactor.Same as expectMsgAllOf(remainingOrDefault, obj...), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgAnyClassOf[C](max: FiniteDuration, obj: Class[_ <: C]*): CReceive one message from the test actor and assert that it conforms to one of the given classes. Receive one message from the test actor and assert that it conforms to one of the given classes. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - returns
- the received object 
 - Definition Classes
- TestKitBase
 
-    def expectMsgAnyClassOf[C](obj: Class[_ <: C]*): CSame as expectMsgAnyClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor.Same as expectMsgAnyClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgAnyOf[T](max: FiniteDuration, obj: T*): TReceive one message from the test actor and assert that it equals one of the given objects. Receive one message from the test actor and assert that it equals one of the given objects. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - returns
- the received object 
 - Definition Classes
- TestKitBase
 
-    def expectMsgAnyOf[T](obj: T*): TSame as expectMsgAnyOf(remainingOrDefault, obj...), but correctly treating the timeFactor.Same as expectMsgAnyOf(remainingOrDefault, obj...), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgClass[C](max: FiniteDuration, c: Class[C]): CReceive one message from the test actor and assert that it conforms to the given class. Receive one message from the test actor and assert that it conforms to the given class. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - returns
- the received object 
 - Definition Classes
- TestKitBase
 
-    def expectMsgClass[C](c: Class[C]): CSame as expectMsgClass(remainingOrDefault, c), but correctly treating the timeFactor.Same as expectMsgClass(remainingOrDefault, c), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectMsgPF[T](max: Duration = Duration.Undefined, hint: String = "")(f: PartialFunction[Any, T]): TReceive one message from the test actor and assert that the given partial function accepts it. Receive one message from the test actor and assert that the given partial function accepts it. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. Use this variant to implement more complicated or conditional processing. - returns
- the received object as transformed by the partial function 
 - Definition Classes
- TestKitBase
 
-    def expectMsgType[T](max: FiniteDuration)(implicit t: ClassTag[T]): TReceive one message from the test actor and assert that it conforms to the given type (after erasure). Receive one message from the test actor and assert that it conforms to the given type (after erasure). Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. - returns
- the received object 
 - Definition Classes
- TestKitBase
 
-    def expectMsgType[T](implicit t: ClassTag[T]): TSame as expectMsgType[T](remainingOrDefault), but correctly treating the timeFactor.Same as expectMsgType[T](remainingOrDefault), but correctly treating the timeFactor.- Definition Classes
- TestKitBase
 
-    def expectNoMessage(): UnitAssert that no message is received. Assert that no message is received. Waits for the default period configured as akka.test.expect-no-message-default. That timeout is scaled using the configuration entry "akka.test.timefactor".- Definition Classes
- TestKitBase
 
-    def expectNoMessage(max: FiniteDuration): UnitAssert that no message is received for the specified time. Assert that no message is received for the specified time. Supplied value is not dilated. - Definition Classes
- TestKitBase
 
-    def expectTerminated(target: ActorRef, max: Duration = Duration.Undefined): TerminatedReceive one message from the test actor and assert that it is the Terminated message of the given ActorRef. Receive one message from the test actor and assert that it is the Terminated message of the given ActorRef. Before calling this method, you have to watchthe target actor ref. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.- target
- the actor ref expected to be Terminated 
- max
- wait no more than max time, otherwise throw AssertionFailure 
- returns
- the received Terminated message 
 - Definition Classes
- TestKitBase
 
-    def fishForMessage(max: Duration = Duration.Undefined, hint: String = "")(f: PartialFunction[Any, Boolean]): AnyHybrid of expectMsgPF and receiveWhile: receive messages while the partial function matches and returns false. Hybrid of expectMsgPF and receiveWhile: receive messages while the partial function matches and returns false. Use it to ignore certain messages while waiting for a specific message. - returns
- the last received message, i.e. the first one for which the partial function returned true 
 - Definition Classes
- TestKitBase
 
-    def fishForSpecificMessage[T](max: Duration = Duration.Undefined, hint: String = "")(f: PartialFunction[Any, T]): TWaits for specific message that partial function matches while ignoring all other messages coming in the meantime. Waits for specific message that partial function matches while ignoring all other messages coming in the meantime. Use it to ignore any number of messages while waiting for a specific one. - returns
- result of applying partial function to the last received message, i.e. the first one for which the partial function is defined 
 - Definition Classes
- TestKitBase
 
-   final  def getClass(): Class[_ <: AnyRef]- Definition Classes
- AnyRef → Any
- Annotations
- @HotSpotIntrinsicCandidate() @native()
 
-    def hashCode(): Int- Definition Classes
- AnyRef → Any
- Annotations
- @HotSpotIntrinsicCandidate() @native()
 
-    def ignoreMsg(f: PartialFunction[Any, Boolean]): UnitIgnore all messages in the test actor for which the given partial function returns true. Ignore all messages in the test actor for which the given partial function returns true. - Definition Classes
- TestKitBase
 
-    def ignoreNoMsg(): UnitStop ignoring messages in the test actor. Stop ignoring messages in the test actor. - Definition Classes
- TestKitBase
 
-   final  def isInstanceOf[T0]: Boolean- Definition Classes
- Any
 
-    def lastSender: ActorRef- Definition Classes
- TestKitBase
 
-    def msgAvailable: BooleanQuery queue status. Query queue status. - Definition Classes
- TestKitBase
 
-   final  def ne(arg0: AnyRef): Boolean- Definition Classes
- AnyRef
 
-   final  def notify(): Unit- Definition Classes
- AnyRef
- Annotations
- @HotSpotIntrinsicCandidate() @native()
 
-   final  def notifyAll(): Unit- Definition Classes
- AnyRef
- Annotations
- @HotSpotIntrinsicCandidate() @native()
 
-    def now: FiniteDurationObtain current time ( System.nanoTime) as Duration.Obtain current time ( System.nanoTime) as Duration.- Definition Classes
- TestKitBase
 
-    def receiveN(n: Int, max: FiniteDuration): Seq[AnyRef]Receive N messages in a row before the given deadline. Receive N messages in a row before the given deadline. - Definition Classes
- TestKitBase
 
-    def receiveN(n: Int): Seq[AnyRef]Same as receiveN(n, remaining)but correctly taking into account Duration.timeFactor.Same as receiveN(n, remaining)but correctly taking into account Duration.timeFactor.- Definition Classes
- TestKitBase
 
-    def receiveOne(max: Duration): AnyRefReceive one message from the internal queue of the TestActor. Receive one message from the internal queue of the TestActor. If the given duration is zero, the queue is polled (non-blocking). This method does NOT automatically scale its Duration parameter! - Definition Classes
- TestKitBase
 
-    def receiveWhile[T](max: Duration = Duration.Undefined, idle: Duration = Duration.Inf, messages: Int = Int.MaxValue)(f: PartialFunction[AnyRef, T]): Seq[T]Receive a series of messages until one does not match the given partial function or the idle timeout is met (disabled by default) or the overall maximum duration is elapsed or expected messages count is reached. Receive a series of messages until one does not match the given partial function or the idle timeout is met (disabled by default) or the overall maximum duration is elapsed or expected messages count is reached. Returns the sequence of messages. Note that it is not an error to hit the maxduration in this case.One possible use of this method is for testing whether messages of certain characteristics are generated at a certain rate: test ! ScheduleTicks(100 millis) val series = receiveWhile(750 millis) { case Tick(count) => count } assert(series == (1 to 7).toList) - Definition Classes
- TestKitBase
 
-    def remaining: FiniteDurationObtain time remaining for execution of the innermost enclosing withinblock or throw an AssertionError if nowithinblock surrounds this call.Obtain time remaining for execution of the innermost enclosing withinblock or throw an AssertionError if nowithinblock surrounds this call.- Definition Classes
- TestKitBase
 
-    def remainingOr(duration: FiniteDuration): FiniteDurationObtain time remaining for execution of the innermost enclosing withinblock or missing that it returns the given duration.Obtain time remaining for execution of the innermost enclosing withinblock or missing that it returns the given duration.- Definition Classes
- TestKitBase
 
-    def remainingOrDefault: FiniteDurationObtain time remaining for execution of the innermost enclosing withinblock or missing that it returns the properly dilated default for this case from settings (key "akka.test.single-expect-default").Obtain time remaining for execution of the innermost enclosing withinblock or missing that it returns the properly dilated default for this case from settings (key "akka.test.single-expect-default").- Definition Classes
- TestKitBase
 
-    def setAutoPilot(pilot: AutoPilot): UnitInstall an AutoPilot to drive the testActor: the AutoPilot will be run for each received message and can be used to send or forward messages, etc. Install an AutoPilot to drive the testActor: the AutoPilot will be run for each received message and can be used to send or forward messages, etc. Each invocation must return the AutoPilot for the next round. - Definition Classes
- TestKitBase
 
-    def shutdown(actorSystem: ActorSystem = system, duration: Duration = Duration.Undefined, verifySystemShutdown: Boolean = false): UnitShut down an actor system and wait for termination. Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system. If verifySystemShutdown is true, then an exception will be thrown on failure. - Definition Classes
- TestKitBase
 
-   final  def synchronized[T0](arg0: => T0): T0- Definition Classes
- AnyRef
 
-   implicit  val system: ActorSystem- Definition Classes
- TestKit → TestKitBase
 
-    lazy val testActor: ActorRefActorRef of the test actor. ActorRef of the test actor. Access is provided to enable e.g. registration as message target. - Definition Classes
- TestKitBase
 
-    def testActorName: StringDefines the testActor name. Defines the testActor name. - Attributes
- protected
- Definition Classes
- TestKitBase
 
-    def testKitSettings: TestKitSettings- Definition Classes
- TestKitBase
 
-    def toString(): String- Definition Classes
- AnyRef → Any
 
-    def unwatch(ref: ActorRef): ActorRefHave the testActor stop watching someone (i.e. Have the testActor stop watching someone (i.e. context.unwatch(...)).- Definition Classes
- TestKitBase
 
-   final  def wait(arg0: Long, arg1: Int): Unit- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
 
-   final  def wait(arg0: Long): Unit- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
 
-   final  def wait(): Unit- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
 
-    def watch(ref: ActorRef): ActorRefHave the testActor watch someone (i.e. Have the testActor watch someone (i.e. context.watch(...)).- Definition Classes
- TestKitBase
 
-    def within[T](max: FiniteDuration)(f: => T): TSame as calling within(0 seconds, max)(f).Same as calling within(0 seconds, max)(f).- Definition Classes
- TestKitBase
 
-    def within[T](min: FiniteDuration, max: FiniteDuration)(f: => T): TExecute code block while bounding its execution time between minandmax.Execute code block while bounding its execution time between minandmax.withinblocks may be nested. All methods in this trait which take maximum wait times are available in a version which implicitly uses the remaining time governed by the innermost enclosingwithinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "akka.test.timefactor", while the min Duration is not. val ret = within(50 millis) { test ! "ping" expectMsgClass(classOf[String]) } - Definition Classes
- TestKitBase
 
Deprecated Value Members
-    def finalize(): Unit- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated
- (Since version 9) 
 
-    def formatted(fmtstr: String): String- Implicit
- This member is added by an implicit conversion from TestKit toStringFormat[TestKit] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @deprecated @inline()
- Deprecated
- (Since version 2.12.16) Use - formatString.format(value)instead of- value.formatted(formatString), or use the- f""string interpolator. In Java 15 and later,- formattedresolves to the new method in String which has reversed parameters.
 
-    def →[B](y: B): (TestKit, B)- Implicit
- This member is added by an implicit conversion from TestKit toArrowAssoc[TestKit] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @deprecated
- Deprecated
- (Since version 2.13.0) Use - ->instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.