I have started writing test for scala actor. I read this blog. http://blog.matthieuguillermin.fr/2013/06/akka-testing-your-actors/ Then I started. I wrote Application Actor. But I realized the application actor which is different others are in blog. Because the actor is as main class. It wrote string on console and sended message another actor. How can I test application actor?
class Application extends Actor{
val cliCommandExecute = context.actorOf(Props[CLICommandExecute],"CLICommandExecute")
println(Util.welcomeMessage)
cliCommandExecute ! CLICommandExecute.Listen(self)
def receive = {
case CLICommandExecute.Done(result: String) => {
println(result)
cliCommandExecute ! CLICommandExecute.Listen(self)
}
case CLICommandExecute.Failed(result: String) => {
println(result)
println(Util.failedMessage)
context.stop(self)
}
case CLICommandExecute.Exit => {
println(Util.exitMessage)
context.stop(self)
}
}
}
I wrote ApplicationTest. But when I run it, test results failed.
class ApplicationTest extends TestKit(ActorSystem("testSystem"))
with WordSpecLike
with Matchers {
"A application actor" must {
// Creation of the TestActorRef
val actorRef = TestActorRef[Application]
val result = "success"
"receive messages" in {
// This call is synchronous. The actor receive() method will be called in the current thread
actorRef ! CLICommandExecute.Done(result)
// This method assert that the testActorRef has received a specific message
expectMsg("success")
}
}
}
Error is as follows:
assertion failed: timeout (3 seconds) during expectMsg while waiting for success
java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsg while waiting for success
How can I proceed?