Could not get the underlyingActor for TestActorRef

24 views
Skip to first unread message

Harit Himanshu

unread,
Jun 1, 2015, 12:13:53 AM6/1/15
to akka...@googlegroups.com
Hey there,

This is what my actor looks like  
object Runner {
  def props(race: Race) = Props(classOf[Runner], race)
}

class Runner(race: Race) extends Actor with ActorLogging {

  import context.dispatcher

  @throws[Exception](classOf[Exception])
  override def postRestart(reason: Throwable): Unit = context.parent ! RestartRunner

  override def receive: Receive = LoggingReceive {
    case Start => {
      log.debug("running...")
      for (i <- 1 to 3) {
        Thread.sleep(200)
      }
      throw new RuntimeException("MarathonRunner is tired")
    }

    case StartWithFuture =>
      log.debug("I am starting to run")
      race.start pipeTo self

    case Failure(throwable) => throw throwable

    case Stop =>
      log.debug("stopping runner")
      context.stop(self)
  }
}
and this is what my test looks
import akka.actor.{Props, ActorSystem}
import akka.testkit.{TestActorRef, TestKit}
import org.scalatest._

class RunnerSpec extends TestKit(ActorSystem("test"))
with WordSpecLike
with MustMatchers {
  "A Runner Actor" must {
    val runner = TestActorRef(Props(new Runner(new Marathon)))
    "receive messages" in {
      runner ! Start
      runner.under <== says Nothing (see attachment)
    }
  }
}
So, when I try to get the underlyingActor, I get `Nothing`, what is wrong here?

Thanks a lot

Martynas Mickevičius

unread,
Jun 4, 2015, 10:31:34 AM6/4/15
to akka...@googlegroups.com
Hi Harit,

as you are passing in Props to the TestActorRef apply method, you will have to specify type of your actor because Props do not have a type parameter:

val runner = TestActorRef[Runner](Props(new Runner(new Marathon)))

Alternatively, you can use a different TestActorRef apply method that accepts a code thunk that returns a new instance of your actor:

val runner = TestActorRef(new Runner(new Marathon))

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

Harit Himanshu

unread,
Jun 4, 2015, 11:21:24 AM6/4/15
to akka...@googlegroups.com
Thank you Martynas, that works
+ Harit

You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/cLmSA1fLZAw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages