问题
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