我的Actor
样子
object LogProcessorActor {
def props(f: () => Unit): Props = Props(new LogProcessorActor(f))
}
class LogProcessorActor(f: () => Unit) extends Actor with ActorLogging {
def receive = LoggingReceive {
case StartLogReaderDisruptor => f()
sender ! StartedLogReaderDisruptor
}
}
我的test
样子
it must "fail with method throws exception" in {
val f: () => Unit = () => 2/0
val logProcessorRef = TestActorRef[LogProcessorActor](LogProcessorActor.props(f), name = "logProcessorSad")
intercept[ArithmeticException] {
logProcessorRef ! StartLogReaderDisruptor
}
}
我在日志中看到抛出异常
[DEBUG] [06/02/2015 19:25:44.598] [ScalaTest-run] [EventStream(akka://logProcessorActorSystem)] logger log1-Slf4jLogger started
[DEBUG] [06/02/2015 19:25:44.599] [ScalaTest-run] [EventStream(akka://logProcessorActorSystem)] Default Loggers started
02 Jun 2015 19:25:44,601 [DEBUG] [logProcessorActorSystem-akka.actor.default-dispatcher-3] akka.event.EventStream| logger log1-Slf4jLogger started
02 Jun 2015 19:25:44,603 [DEBUG] [logProcessorActorSystem-akka.actor.default-dispatcher-3] akka.event.EventStream| Default Loggers started02 Jun 2015 19:25:44,782 [ERROR] [logProcessorActorSystem-akka.actor.default-dispatcher-3] akka.actor.OneForOneStrategy| / by zero
java.lang.ArithmeticException: / by zero
at com.shn.lp.LogProcessorActorSpec$$anonfun$3$$anonfun$4.apply$mcV$sp(LogProcessorActorSpec.scala:18)
at com.shn.lp.LogProcessorActor$$anonfun$receive$1.applyOrElse(LogProcessorActor.scala:16)
at akka.actor.Actor$class.aroundReceive(Actor.scala:467)
at com.shn.lp.LogProcessorActor.aroundReceive(LogProcessorActor.scala:14)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
但我的测试仍然失败
Expected exception java.lang.ArithmeticException to be thrown, but no exception was thrown.
ScalaTestFailureLocation: com.shn.lp.LogProcessorActorSpec$$anonfun$3 at (LogProcessorActorSpec.scala:20)
org.scalatest.exceptions.TestFailedException: Expected exception java.lang.ArithmeticException to be thrown, but no exception was thrown.
我什至尝试了docs中提到的策略,但我仍然得到相同的结果。
我在做什么不正确?