Testing 使用Akka Actor参数测试一种方法
我有一个任务,我需要测试。此任务将一些参与者作为参数,因此我使用Testing 使用Akka Actor参数测试一种方法,testing,akka,actor,Testing,Akka,Actor,我有一个任务,我需要测试。此任务将一些参与者作为参数,因此我使用TestActor创建了一个测试,如下所示: 几个问题: 1.为测试目的创建ActorSystem是否为正常做法? 2.代码没有结束。我已经试过了 val system = ActorSystem("TestRoutingSystem") val actorRef = system.actorOf(Props(new TestActor), name = "testActor") var poisonReceived = fal
TestActor
创建了一个测试,如下所示:
几个问题:
1.为测试目的创建ActorSystem
是否为正常做法?
2.代码没有结束。我已经试过了
val system = ActorSystem("TestRoutingSystem")
val actorRef = system.actorOf(Props(new TestActor), name = "testActor")
var poisonReceived = false
var workReceived = 0
class TestActor extends Actor with Logging {
def receive = {
case msg: WorkerMessage =>
workReceived += 1
case x: Boolean => {
poisonReceived = true
}
}
}
val t = new java.util.Timer()
val task = new TestTask(...)
task.run()
t.schedule(task, 10, 10)
system.shutdown()
Thread.sleep(150)
println(poisonReceived )
println(workReceived)
正如@nafr所说,最好使用
akka testkit
我不熟悉scalatest,所以我用junit风格编写示例。例如,我们应该测试某个类,该类向actor发送两条消息:
class TimerTask(actorRef: ActorRef) {
def run(): Unit = {
actorRef ! "some msg"
actorRef ! "some other msg"
}
}
完整的测试将如下所示:
import akka.actor.{ActorRef, ActorSystem}
import akka.testkit.{TestKit, TestProbe}
import org.junit.{After, Before, Test}
class TestExample {
protected implicit var system: ActorSystem = _
@Before
def setup(): Unit = {
system = ActorSystem.create("test")
}
@After
def tearDown(): Unit = {
TestKit.shutdownActorSystem(system)
}
@Test
def example(): Unit = {
val testProbe = TestProbe()
val source = new TimerTask(testProbe.ref)
source.run()
testProbe.expectMsg("some msg")
testProbe.expectMsg("some other msg")
}
}
因此,请回答您的问题:1) 只有演员参与,这才是真正的测试方式
2) 使用
TestKit。出于测试目的,强烈建议使用akka TestKit模块@nafr我不是在测试参与者。我正在测试另一个以Actor为参数的方法。它是什么意思“代码没有结束”?如何运行测试(junit,scalatest)?@zella我认为后台计时器
线程t
正在运行,因为它没有被取消。这是一个测试代码,尽管我没有将其插入测试框架,因为我发现它永远都在继续。