pekko/akka-testkit/src/test/scala/akka/testkit/AkkaSpec.scala

42 lines
1.3 KiB
Scala
Raw Normal View History

2011-10-10 11:12:34 +02:00
/**
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.testkit
import akka.config.Configuration
import org.scalatest.{ WordSpec, BeforeAndAfterAll }
2011-10-10 11:12:34 +02:00
import org.scalatest.matchers.MustMatchers
import akka.AkkaApplication
import akka.actor.{ Actor, ActorRef, Props }
2011-10-11 16:05:48 +02:00
import akka.dispatch.MessageDispatcher
2011-10-10 11:12:34 +02:00
abstract class AkkaSpec(_application: AkkaApplication = AkkaApplication())
extends TestKit(_application) with WordSpec with MustMatchers with BeforeAndAfterAll {
final override def beforeAll {
atStartup()
}
final override def afterAll {
app.stop()
atTermination()
}
protected def atStartup() {}
protected def atTermination() {}
2011-10-10 11:12:34 +02:00
def this(config: Configuration) = this(new AkkaApplication(getClass.getSimpleName, AkkaApplication.defaultConfig ++ config))
2011-10-18 17:56:23 +02:00
def actorOf(props: Props): ActorRef = app.actorOf(props)
2011-10-18 17:56:23 +02:00
def actorOf[T <: Actor](clazz: Class[T]): ActorRef = actorOf(Props(clazz))
2011-10-18 17:56:23 +02:00
def actorOf[T <: Actor: Manifest]: ActorRef = actorOf(manifest[T].erasure.asInstanceOf[Class[_ <: Actor]])
2011-10-18 17:56:23 +02:00
def actorOf[T <: Actor](factory: T): ActorRef = actorOf(Props(factory))
2011-10-11 16:05:48 +02:00
def spawn(body: Unit)(implicit dispatcher: MessageDispatcher) {
2011-10-18 17:56:23 +02:00
actorOf(Props(ctx { case "go" try body finally ctx.self.stop() }).withDispatcher(dispatcher)) ! "go"
2011-10-11 16:05:48 +02:00
}
2011-10-10 11:12:34 +02:00
}