Creating a TestActorRef results in NullPointerException

喜欢而已 提交于 2019-12-22 10:11:54

问题


I am trying to get a TestActorRef like that

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
    }
  }

the this actor

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

  def receive = {
  [...]
  }
}

But this leaves me with the following stacktrace

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

Edit: It seems to have something to do with this actor in particular. Getting a TestActorRef to another actor class is working correctly. I read that there was a problem with TextActorRefs for actors that have the Stash trait, but this was said to be resolved in the current version. (Reference)

Edit2: Ok. I was wrong. The current release is not 2.3. So I have to wait?!


回答1:


Verified that upgrading to akka 2.3.0 is the correct answer for fixing TestActorRef with the Stash trait.




回答2:


Instantiate the actor:

val actorRef = TestActorRef(new NotifySender())

That's how I always go about it anyway. :)



来源:https://stackoverflow.com/questions/21725473/creating-a-testactorref-results-in-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!