![](/img/trans.png)
[英]Scala and Akka - Testing actors as a system with Akka Testkit
[英]Scala Testkit Unit Testing with Akka
我正在使用TestKit測試一個我正在參與Akka Actors的Scala項目的某些類,並且遇到了這個問題:
One or more requested classes are not Suites: poc.PocConstituentsWatcher
有問題的類如下所示:
class PocConstituentsWatcher(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfter with BeforeAndAfterAll {
我以前沒有這個問題,因為
def this() = this(ActorSystem)
但是現在我通過注入定義了自己的ActorSystem,所以我val actorSystem = injector.instance[ActorSystem]
def this() = this(actorSystem)
我收到一條錯誤消息,指出找不到actorSystem。 我認為是因為構造函數簽名不正確? 謝謝你的幫助。
編輯:
展示了如何注入演員系統。
val injector = Guice.createInjector(new AkkaModule)
val actorSystem = injector.instance[ActorSystem]
在AkkaModule中
object AkkaModule {
class ActorSystemProvider @Inject() (val config: Config, val injector: Injector) extends Provider[ActorSystem] {
override def get() = {
val system = ActorSystem("poc-actor-system", config)
GuiceAkkaExtension(system).initialize(injector)
system
}
}
}
class AkkaModule extends AbstractModule with ScalaModule {
override def configure() {
bind[ActorSystem].toProvider[ActorSystemProvider].asEagerSingleton()
}
}
我最終使用
def this() = this(ActorSystem("poc-actor-system"))
可以工作,但是有點令人擔憂,因為從技術上講,默認構造函數不使用與我創建的那個相同的actor系統val actorSystem = injector.instance[ActorSystem]
。 如果將來發生任何問題,將向您報告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.