diff --git a/akka-docs/scala/testing.rst b/akka-docs/scala/testing.rst index d2875ed62a..d19a1ab753 100644 --- a/akka-docs/scala/testing.rst +++ b/akka-docs/scala/testing.rst @@ -685,6 +685,11 @@ The ``implicit lazy val system`` must be declared exactly like that (you can course pass arguments to the actor system factory as needed) because trait :class:`TestKitBase` needs the system during its construction. +.. warning:: + + Use of the trait is discouraged because of potential issues with binary + backwards compatibility in the future, use at own risk. + Specs2 ------ diff --git a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala index 9dfa40a5ee..373f4c1fff 100644 --- a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala +++ b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala @@ -62,44 +62,22 @@ class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor { } /** - * 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. + * Implementation trait behind the [[akka.testkit.TestKit]] class: you may use + * this if inheriting from a concrete class is not possible. * - *
- * 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.shutdown()
- * }
+ * Use of the trait is discouraged because of potential issues with binary
+ * backwards compatibility in the future, use at own risk.
+ *
+ * This trait requires the concrete class mixing it in to provide an
+ * [[akka.actor.ActorSystem]] which is available before this traits’s
+ * constructor is run. The recommended way is this:
+ *
+ * {{{
+ * class MyTest extends TestKitBase {
+ * implicit lazy val system = ActorSystem() // may add arguments here
+ * ...
* }
- *
- *
- * 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); 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".
- *
- * @author Roland Kuhn
- * @since 1.1
+ * }}}
*/
trait TestKitBase {
@@ -579,6 +557,46 @@ trait TestKitBase {
private def format(u: TimeUnit, d: Duration) = "%.3f %s".format(d.toUnit(u), u.toString.toLowerCase)
}
+/**
+ * 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(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.shutdown()
+ * }
+ * }
+ *
+ *
+ * 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); 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".
+ *
+ * @author Roland Kuhn
+ * @since 1.1
+ */
class TestKit(_system: ActorSystem) extends { implicit val system = _system } with TestKitBase
object TestKit {