【问题标题】:Creating a TestActorRef results in NullPointerException创建 TestActorRef 会导致 NullPointerException
【发布时间】:2014-02-12 10:39:13
【问题描述】:

我正在尝试获得这样的 TestActorRef

class NotifySenderTest(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfter {

  def this() = this(ActorSystem("NotifySenderTest"))
  override def afterAll {
    TestKit.shutdownActorSystem(system)
  }

  "A NotifySender" must {
    "be able to process the required messages" in {
      val actorRef = TestActorRef[NotifySender] //Line 92
    }
  }

这个演员

class NotifySender extends Actor with Stash {
  import Tcp._
  import context.system

  def receive = {
  [...]
  }
}

但这给我留下了以下堆栈跟踪

java.lang.NullPointerException:在 akka.actor.dungeon.Dispatch$class.init(Dispatch.scala:62) 在 akka.actor.ActorCell.init(ActorCell.scala:338) 在 akka.actor.LocalActorRef.(ActorRef.scala:304) 在 akka.testkit.TestActorRef.(TestActorRef.scala:21) 在 akka.testkit.TestActorRef$.apply(TestActorRef.scala:141) 在 akka.testkit.TestActorRef$.apply(TestActorRef.scala:137) 在 akka.testkit.TestActorRef$.apply(TestActorRef.scala:146) 在 akka.testkit.TestActorRef$.apply(TestActorRef.scala:144) 在 actor.NotifySenderTest$$anonfun$2$$anonfun$apply$mcV$sp$4.apply$mcV$sp(NotifySenderTest.scala:92) 在 actor.NotifySenderTest$$anonfun$2$$anonfun$apply$mcV$sp$4.apply(NotifySenderTest.scala:91) ...

编辑:这似乎与这个演员特别有关。将 TestActorRef 获取到另一个演员类工作正常。我读到对于具有 Stash 特征的演员来说 TextActorRefs 存在问题,但据说在当前版本中已解决。 (Reference)

Edit2:好的。我错了。当前版本不是 2.3。所以我要等?!

【问题讨论】:

  • 是的,这个问题已在 2.3.0-RC2 中修复,但由于二进制兼容性问题,我们无法将其反向移植到 2.2.x。
  • 你知道2.3.0稳定版的发布日期吗?
  • “知道”很难,因为它在于未来,但目前的计划是两周后,因为我现在正在准备 RC3。

标签: scala testing akka actor


【解决方案1】:

已验证升级到 akka 2.3.0 是使用 Stash 特征修复 TestActorRef 的正确答案。

【讨论】:

  • 为了让我能够将StashTestActorRef 一起使用,我还必须更改调度程序。如果不更改调度程序,unstashAll() 将无法工作。看到这个answer
【解决方案2】:

实例化演员:

val actorRef = TestActorRef(new NotifySender())

反正我总是这样。 :)

【讨论】:

  • 我已经试过这个导致这个堆栈跟踪:gist.github.com/anonymous/8532ca52c6d53b88e6f9
  • 是的,问题肯定出在Stash,正如您在编辑中已经指出的那样。我的团队使用 2.2.3,但我之前没有使用过Stash。它是否适用于该 akka 版本,或者您是否依赖于某些 2.3 功能?
  • 我也在使用 akka 2.2.3,但在这个版本中,无法为具有 stash 特征的演员创建​​ TestActorRef。因此,需要等待 2.3 版本出来。
猜你喜欢
  • 2016-12-19
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 2016-03-07
  • 2017-05-16
相关资源
最近更新 更多