akka.testkit

TestKit

trait TestKit extends AnyRef

Test kit for testing actors. Inheriting from this trait 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 {
    val test = actorOf[SomeActor].start()

    within (1 second) {
      test ! SomeWork
      expectMsg(Result1) // bounded to 1 second
      expectMsg(Result2) // bounded to the remainder of the 1 second
    }
}

Beware of two points:

Since

1.1

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. TestKit
  2. AnyRef
  3. Any
Visibility
  1. Public
  2. All

Value Members

  1. def != (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  2. def != (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  3. def ## (): Int

    Attributes
    final
    Definition Classes
    AnyRef → Any
  4. def == (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  5. def == (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  6. def asInstanceOf [T0] : T0

    Attributes
    final
    Definition Classes
    Any
  7. def clone (): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  8. def eq (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  9. def equals (arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def expectMsg [T] (max: Duration)(f: PartialFunction[Any, T]): T

    Receive 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

  11. def expectMsg [T] (f: PartialFunction[Any, T]): T

    Same as expectMsg, but takes the maximum wait time from the innermost enclosing within block.

  12. def expectMsg (max: Duration, obj: Any): AnyRef

    Receive 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

  13. def expectMsg (obj: Any): AnyRef

    Same as expectMsg, but takes the maximum wait time from the innermost enclosing within block.

  14. def expectMsgAllClassOf (max: Duration, obj: Class[_]*): Unit

    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.

  15. def expectMsgAllClassOf (obj: Class[_]*): Unit

    Same as expectMsgAllClassOf, but takes the maximum wait time from the innermost enclosing within block.

  16. def expectMsgAllConformingOf (max: Duration, obj: Class[_]*): Unit

    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.

    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. 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.

  17. def expectMsgAllConformingOf (obj: Class[_]*): Unit

    Same as expectMsgAllConformingOf, but takes the maximum wait time from the innermost enclosing within block.

  18. def expectMsgAllOf (max: Duration, obj: Any*): Unit

    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.

    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. 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.

    within(1 second) {
      dispatcher ! SomeWork1()
      dispatcher ! SomeWork2()
      expectMsgAllOf(Result1(), Result2())
    }
    

  19. def expectMsgAllOf (obj: Any*): Unit

    Same as expectMsgAllOf, but takes the maximum wait time from the innermost enclosing within block.

  20. def expectMsgAnyClassOf (max: Duration, obj: Class[_]*): AnyRef

    Receive 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

  21. def expectMsgAnyClassOf (obj: Class[_]*): AnyRef

    Same as expectMsgAnyClassOf, but takes the maximum wait time from the innermost enclosing within block.

  22. def expectMsgAnyOf (max: Duration, obj: Any*): AnyRef

    Receive 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

  23. def expectMsgAnyOf (obj: Any*): AnyRef

    Same as expectMsgAnyOf, but takes the maximum wait time from the innermost enclosing within block.

  24. def expectMsgClass [C] (max: Duration, c: Class[C]): C

    Receive 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

  25. def expectMsgClass [C] (c: Class[C]): C

    Same as expectMsgClass, but takes the maximum wait time from the innermost enclosing within block.

  26. def expectNoMsg (max: Duration): Unit

    Assert that no message is received for the specified time.

  27. def expectNoMsg : Unit

    Same as expectNoMsg, but takes the maximum wait time from the innermost enclosing within block.

  28. def finalize (): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  29. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef
  30. def hashCode (): Int

    Definition Classes
    AnyRef → Any
  31. def ignoreMsg (f: PartialFunction[AnyRef, Boolean]): Unit

    Ignore all messages in the test actor for which the given partial function returns true.

  32. def ignoreNoMsg : Unit

    Stop ignoring messages in the test actor.

  33. def isInstanceOf [T0] : Boolean

    Attributes
    final
    Definition Classes
    Any
  34. def ne (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  35. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  36. def notifyAll (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  37. def now : Duration

    Obtain current time (System.currentTimeMillis) as Duration.

  38. def receiveWhile [T] (max: Duration)(f: PartialFunction[AnyRef, T]): Seq[T]

    Receive a series of messages as long as the given partial function accepts them or the idle timeout is met or the overall maximum duration is elapsed.

    Receive a series of messages as long as the given partial function accepts them or the idle timeout is met or the overall maximum duration is elapsed. Returns the sequence of messages.

    Beware that the maximum duration is not implicitly bounded by or taken from the innermost enclosing within block, as it is not an error to hit the max duration 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)
    

  39. def receiveWhile [T] (f: PartialFunction[AnyRef, T]): Seq[T]

    Same as receiveWhile, but takes the maximum wait time from the innermost enclosing within block.

  40. def remaining : Duration

    Obtain time remaining for execution of the innermost enclosing within block.

  41. implicit val senderOption : Some[ActorRef]

    Implicit sender reference so that replies are possible for messages sent from the test class.

    Implicit sender reference so that replies are possible for messages sent from the test class.

    Attributes
    protected implicit
  42. def setTestActorTimeout (d: Duration): Unit

    Set test actor timeout.

    Set test actor timeout. By default, the test actor shuts itself down after 5 seconds of inactivity. Set this to Duration.Inf to disable this behavior, but make sure that someone will then call stopTestActor, unless you want to leak actors, e.g. wrap test in

      try {
        ...
      } finally { stopTestActor }
    

  43. def stopTestActor : Unit

    Stop test actor.

    Stop test actor. Should be done at the end of the test unless relying on test actor timeout.

  44. def synchronized [T0] (arg0: ⇒ T0): T0

    Attributes
    final
    Definition Classes
    AnyRef
  45. val testActor : ActorRef

    ActorRef of the test actor.

    ActorRef of the test actor. Access is provided to enable e.g. registration as message target.

    Attributes
    protected
  46. def toString (): String

    Definition Classes
    AnyRef → Any
  47. def wait (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  48. def wait (arg0: Long, arg1: Int): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  49. def wait (arg0: Long): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  50. def within [T] (max: Duration)(f: ⇒ T): T

    Same as calling within(0 seconds, max)(f).

  51. def within [T] (min: Duration, max: Duration)(f: ⇒ T): T

    Execute code block while bounding its execution time between min and max.

    Execute code block while bounding its execution time between min and max. within blocks 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 enclosing within block.

    val ret = within(50 millis) {
            test ! "ping"
            expectMsgClass(classOf[String])
          }
    

Inherited from AnyRef

Inherited from Any