class JavaTestKit extends AnyRef
Java API: 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.
Beware of two points:
- the ActorSystem passed into the constructor needs to be shutdown,
otherwise thread pools and memory will be leaked - this trait is not
thread-safe (only one actor with one queue, one stack of
Within
blocks); 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 the
dilated
method, which uses the TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry "akka.test.timefactor".
- Source
- JavaTestKit.java
- Alphabetic
- By Inheritance
- JavaTestKit
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new JavaTestKit(system: ActorSystem)
Type Members
-
abstract
class
AwaitAssert
extends AnyRef
Await until the given assert does not throw an exception or the timeout expires, whichever comes first.
Await until the given assert does not throw an exception or the timeout expires, whichever comes first. If the timeout expires the last exception is thrown.
If no timeout is given, take it from the innermost enclosing
Within
block.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "akka.test.timefactor".
-
abstract
class
AwaitCond
extends AnyRef
Await until the given condition evaluates to
true
or the timeout expires, whichever comes first.Await until the given condition evaluates to
true
or the timeout expires, whichever comes first.If no timeout is given, take it from the innermost enclosing
Within
block.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "akka.test.timefactor".
-
abstract
class
EventFilter
[T] extends AnyRef
Facilities for selectively filtering out expected events from logging so that you can keep your test run’s console output clean and do not miss real error messages.
Facilities for selectively filtering out expected events from logging so that you can keep your test run’s console output clean and do not miss real error messages.
If the
occurrences
is set toInteger.MAX_VALUE
, no tracking is done. -
abstract
class
ExpectMsg
[T] extends AnyRef
Receive one message from the test actor and assert that the given matching function accepts it.
Receive one message from the test actor and assert that the given matching function accepts it. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.
The received object as transformed by the matching function can be retrieved with the
get
method.Use this variant to implement more complicated or conditional processing.
final String out = new ExpectMsg<String>("match hint") { protected String match(Object in) { if (in instanceof Integer) return "match"; else throw noMatch(); } }.get(); // this extracts the received message
-
abstract
class
IgnoreMsg
extends AnyRef
Ignore all messages in the test actor for which the given function returns true.
-
abstract
class
ReceiveWhile
[T] extends AnyRef
Receive a series of messages until one does not match the given
match
function or the idle timeout is met (disabled by default) or the overall maximum duration is elapsed.Receive a series of messages until one does not match the given
match
function or the idle timeout is met (disabled by default) or the overall maximum duration is elapsed. Returns the sequence of messages.Note that 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.
-
abstract
class
Within
extends AnyRef
Execute code block while bounding its execution time between
min
andmax
.Execute code block while bounding its execution time between
min
andmax
.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 enclosingWithin
block.Note that the timeout is scaled using
dilated
, which uses the configuration entry "akka.test.timefactor", while the min Duration is not.// the run() method needs to finish within 3 seconds new Within(duration("3 seconds")) { protected void run() { // ... } }
Value Members
-
def
childActorOf(props: Props): ActorRef
Spawns 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. The actor will have an auto-generated name and will be supervised using
SupervisorStrategy.stoppingStrategy
.- props
Props to create the child actor
-
def
childActorOf(props: Props, name: String): ActorRef
Spawns 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. The actor will be supervised using
SupervisorStrategy.stoppingStrategy
.- props
Props to create the child actor
- name
Actor name for the child actor
-
def
childActorOf(props: Props, supervisorStrategy: SupervisorStrategy): ActorRef
Spawns 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. The actor will have an auto-generated name.
- props
Props to create the child actor
- supervisorStrategy
Strategy should decide what to do with failures in the actor.
-
def
childActorOf(props: Props, name: String, supervisorStrategy: SupervisorStrategy): ActorRef
Spawns 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.
- def dilated(d: Duration): Duration
-
def
expectMsgAllOf(max: FiniteDuration, msgs: <repeated...>[AnyRef]): Array[AnyRef]
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.
-
def
expectMsgAllOf(msgs: <repeated...>[AnyRef]): Array[AnyRef]
Same as
expectMsgAllOf(remainingOrDefault(), obj...)
, but correctly treating the timeFactor. -
def
expectMsgAnyClassOf(max: FiniteDuration, classes: <repeated...>[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
-
def
expectMsgAnyClassOf[T](classes: <repeated...>[Class[_ <: T]]): T
Same as
expectMsgAnyClassOf(remainingOrDefault(), obj...)
, but correctly treating the timeFactor. -
def
expectMsgAnyOf(max: FiniteDuration, msgs: <repeated...>[AnyRef]): 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
-
def
expectMsgAnyOf(msgs: <repeated...>[AnyRef]): AnyRef
Same as
expectMsgAnyOf(remainingOrDefault(), obj...)
, but correctly treating the timeFactor. -
def
expectMsgClass[T](max: FiniteDuration, clazz: Class[T]): T
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
-
def
expectMsgClass[T](clazz: Class[T]): T
Same as
expectMsgClass(remainingOrDefault(), clazz)
, but correctly treating the timeFactor. -
def
expectMsgEquals[T](max: FiniteDuration, msg: T): T
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
-
def
expectMsgEquals[T](msg: T): T
Same as
expectMsgEquals(remainingOrDefault(), obj)
, but correctly treating the timeFactor. -
def
expectNoMsg(max: FiniteDuration): Unit
Assert that no message is received for the specified time.
-
def
expectNoMsg(): Unit
Same as
expectNoMsg(remainingOrDefault())
, but correctly treating the timeFactor. -
def
expectTerminated(target: ActorRef): Terminated
Same as
expectTerminated(remainingOrDefault(), target)
, but correctly treating the timeFactor.Same as
expectTerminated(remainingOrDefault(), target)
, but correctly treating the timeFactor. Don't forget to 'watch' it first! -
def
expectTerminated(max: Duration, target: ActorRef): Terminated
Assert that the given ActorRef is Terminated within the specified time.
Assert that the given ActorRef is Terminated within the specified time. Don't forget to 'watch' it first!
- def forward(actor: ActorRef): Unit
- def getLastSender(): ActorRef
-
def
getRef(): ActorRef
Shorthand to get the testActor.
- def getRemainingTime(): FiniteDuration
- def getRemainingTimeOr(def: FiniteDuration): FiniteDuration
- def getSystem(): ActorSystem
-
def
getTestActor(): ActorRef
ActorRef of the test actor.
ActorRef of the test actor. Access is provided to enable e.g. registration as message target.
-
def
ignoreNoMsg(): Unit
Stop ignoring messages in the test actor.
-
def
msgAvailable(): Boolean
Query queue status.
-
def
receiveN(n: Int, max: FiniteDuration): Array[AnyRef]
Receive N messages in a row before the given deadline.
-
def
receiveN(n: Int): Array[AnyRef]
Same as
receiveN(n, remaining())
, but correctly treating the timeFactor. -
def
receiveOne(max: Duration): AnyRef
Receive 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!
-
def
remaining(): FiniteDuration
Obtain time remaining for execution of the innermost enclosing
Within
block or missing that it returns the properly dilated default for this case from settings (key "akka.test.single-expect-default"). -
def
remainingOr(duration: FiniteDuration): FiniteDuration
Obtain time remaining for execution of the innermost enclosing
Within
block or missing that it returns the given duration. -
def
remainingOrDefault(): FiniteDuration
Obtain time remaining for execution of the innermost enclosing
Within
block or missing that it returns the properly dilated default for this case from settings (key "akka.test.single-expect-default"). - def reply(msg: Any): Unit
- def send(actor: ActorRef, msg: Any): Unit
-
def
setAutoPilot(pilot: AutoPilot): Unit
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.
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.
- def shutdown(actorSystem: ActorSystem, verifySystemShutdown: Boolean): Unit
- def shutdown(actorSystem: ActorSystem, duration: Duration): Unit
- def shutdown(actorSystem: ActorSystem): Unit
-
def
shutdown(actorSystem: ActorSystem, duration: Duration, verifySystemShutdown: Boolean): Unit
Shut 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.
-
def
unwatch(ref: ActorRef): ActorRef
Have the testActor stop watching someone (i.e.
Have the testActor stop watching someone (i.e.
getContext.unwatch(...)
). -
def
watch(ref: ActorRef): ActorRef
Have the testActor watch someone (i.e.
Have the testActor watch someone (i.e.
getContext().getWatch(...)
).