Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FloWi/55572324f2d3adb3d51b to your computer and use it in GitHub Desktop.
Save FloWi/55572324f2d3adb3d51b to your computer and use it in GitHub Desktop.
package utils
import akka.actor.{Actor, ActorSystem}
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import org.scalatest.FunSuiteLike
import scala.concurrent.duration._
class StatefulnessBetweenTestsWithImplicitSenderSpec extends TestKit(ActorSystem("MyTestSystem"))
with FunSuiteLike
with ImplicitSender {
test("1st test") {
val testActorRef: TestActorRef[MyActorWithState] = TestActorRef[MyActorWithState]
testActorRef ! "Hello"
//sending the message and watching asserting the right state of the underlying actor
//the answer is not being consumed by some expect-method
assert(testActorRef.underlyingActor.state == "Hello")
}
test("2nd test") {
val testActorRef: TestActorRef[MyActorWithState] = TestActorRef[MyActorWithState]
testActorRef ! "World"
//expecting a message --> taking the first message off the mailbox
val answer = expectMsgType[String](10.milliseconds)
assert(answer == "World")
}
}
class MyActorWithState extends Actor {
var state: String = _
override def receive: Receive = {
case x: String =>
state = x
sender() ! state
}
}
/*
Test-Output
[info] StatefulnessBetweenTestsWithImplicitSenderSpec:
[info] - 1st test
[info] - 2nd test *** FAILED ***
[info] "[Hello]" did not equal "[World]" (StatefulnessBetweenTestsWithImplicitSenderSpec.scala:30)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment