将以下存根与 Akka Testkit 一起使用时出现问题:
import akka.actor.{Props, Status}
import akka.camel.{CamelExtension, CamelMessage, Consumer}
import akka.testkit.{ImplicitSender, TestProbe}
import org.scalatest._
[import own packages]
import scala.concurrent.duration.DurationInt
import scala.language.postfixOps
class ActorSpec extends AkkaTestBase
with ImplicitSender with WordSpecLike
with Matchers with BeforeAndAfterAll with IOSugars {
var stubResponse: String = ""
var doNotRespond = false
val stubEndpoint = "direct:test"
val reportProbe = TestProbe()
val actorUnderTest= TestProbe()
// create stub consumer
val stubConsumer = system.actorOf(Props(new Consumer {
override val endpointUri = stubEndpoint
override def replyTimeout = 1 second
override def receive: Receive = {
case msg: CamelMessage if doNotRespond => ()
case msg: CamelMessage => sender() ! stubResponse;
case _ => sender() ! "ack"
}
}), "stubActor")
[...]
}
当我运行以下测试用例时:
"Respond with failure when camel produces a failure" in {
doNotRespond = true
actorUnderTest ! // Send relevant message
// (which returns the () above because of doNotRespond set to true)
doNotRespond = false // Return to original state
testProbe.expectMsg // Expect relevant message
"Respond with a failure when 'ERROR' status code is returned" in {
stubResponse = readStringFromResource("/failure.xml")
actorUnderTest ! // send relevant message
expectMsgClass(classOf[Status.Failure])
}
不幸的是,这不起作用。我们得到一个超时:
错误 oacprocessor.DefaultErrorHandler - (MessageId:
xxx on ExchangeId: xxx) 的传递失败。交付尝试后用尽:1 捕获:
java.util.concurrent.TimeoutException:无法
在超时 [1 秒] 内从参与者 [ActorEndpointPath(akka://xxx/stubActor] 获得响应。
当将更改doNotRespond
状态变量的测试用例作为最后一个测试用例时,一切正常。
为什么会这样,如何解决?