score:10
Okay, I figured it out. Make sure you are using the correct version of akka-testkit. In Play 2.2.0 I was trying to use akka 2.2.M3. Obviously, that doesn't work. I had to put the correct dependencies in my Build.scala, which ended up being this:
"com.typesafe.akka" %% "akka-testkit" % "2.2.0" % "test"
My actual test code looks like this:
package test
import org.specs2.mutable._
import controllers.Scanner
import java.util.UUID
import org.joda.time.DateTime
import akka.testkit.TestActorRef
import play.api.Logger
import models.PSqlEnum
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import scala.concurrent.ExecutionContext.Implicits.global
class ScannerTest extends Specification {
val appId = UUID.randomUUID()
val app = models.App(appId, "TestApp", "TestServer", "TestComponent", "Test Description", DateTime.now(),
DateTime.now(), true, 3, 60, PSqlEnum("scanType", "mandatory"), "http://localhost")
val rules = <Rule name="DivisionDataIsAvailable" elementsToCheck="DivisionDataIsAvailable"
ruleType="is, true, yellow" />
<Rule name="DivisionDataLoadIsHealthy" elementsToCheck="DivisionDataLoadIsHealthy"
ruleType="is, true, red" />;
"Scanner" should {
"test something" in {
implicit val actorSystem = ActorSystem("testActorSystem", ConfigFactory.load())
val scanner = TestActorRef(new Scanner(app, rules)).underlyingActor
val response = scanner.getResponseFromWebService
response onSuccess {
case result => Logger.warn(result.toString)
}
response onFailure {
case error => Logger.warn(error.toString)
}
1 === 1
}
}
}
Obviously again, this test isn't really doing anything. The actual test being evaluated is 1 === 1. It does print out to the log now though which means I can go back and verify datatypes and the payload of the response, and then build some actual Unit Tests. I hope someone finds this useful. Those error messages in the original question are caused by the akka-testkit dependency not being the same version as Akka though, which might be useful for someone.
score:0
I'm not using spec2 or Mockito but here is what I'm doing to unit test Akka actors in Play 2.2.1:
import org.specs2.mutable.Specification
import scala.concurrent.duration._
import org.scalatest.concurrent._
import akka.testkit._
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{FlatSpec, BeforeAndAfterAll}
import akka.actor.{Props, ActorSystem}
import akka.pattern.ask
import akka.util.Timeout
import scala.util.{Failure, Success}
import model.BlacklistEntry
import scala.concurrent.{Future, Promise, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.test.FakeApplication
import model.BlacklistEntryImpl
class LicenceBlackListSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with ShouldMatchers with FlatSpec with BeforeAndAfterAll {
play.api.Play.start(FakeApplication())
import akka.testkit.TestKit._
def this() = this( ActorSystem("LicenceBlackListSpec") )
override def afterAll: Unit = {
system.shutdown()
system.awaitTermination(10.seconds)
}
implicit val timeout = Timeout(10 seconds)
val blacklistRef = TestActorRef(Props[LicenceBlackList])
"An LicenceBlackList Actor" should "be able to create a new blacklist entry" in {
blacklistRef ! CreateEntry(BlacklistEntryImpl("NEW_KEY",1000,"Test creation"))
val expected: BlacklistEntry = BlacklistEntryImpl("NEW_KEY", 1000 ,"Test creation")
expectMsg( expected )
}
}
You'll need to include the scalatest lib as a dependency as well akka test kit:
"org.scalatest" % "scalatest_2.10" % "1.9.1"
Hope this will help.
score:0
I don't use ActorSystem("testActorSystem" ) , I have tryed to allow play framework to use it's usual akka plugin. This approach has folowing benefits: I can use play.api.libs.concurrent.Akka.system in all code, I can have extra config options that understand booth play and akka code parts .
class BaseActorTester (_app: Application) extends akka.testkit.TestKit(play.api.libs.concurrent.Akka.system(_app)) with FunSuiteLike with BeforeAndAfterAll {
def this() = this(FakeApplication(additionalConfiguration=Map("currency.db"->"airando-test")))
implicit val app: Application = _app
implicit val ec = play.api.libs.concurrent.Akka.system.dispatcher
override def beforeAll {
Play.start(app)
}
// play plugin do it itself ??
//override def afterAll {
// akka.testkit.TestKit.shutdownActorSystem(system)
//}
...
}
Source: stackoverflow.com
Related Query
- How do I unit test akka actors in Play Framework 2.2.0 Scala (spec2, Mockito)
- Play Framework 2 scala specs2 mockito, how do I write a mocking unit test
- How do I unit test a controller in play framework 2 scala
- scala play framework how to unit test async controllers
- How to test a Scala Play Framework websocket?
- how can I remove codes creating session in unit test for Play framework and slick
- Unit Test controllers in Play 2 framework with Scala
- How to write unit test for Play Framework JSON readers
- How to intercept and change exception in Scala Akka in play framework 2.4.6
- How to write Java TestNG test class for scala play framework project?
- Scala simple funsuite unit test with akka actors fails
- How can I unit test this Play Scala controller using Specs2?
- How to use IntelliJ with Play Framework and Scala
- How do I write a scala unit test that ensures compliation fails?
- How do you unit test Scala in Eclipse?
- Scala / Lift: How do I write unit tests that test a snippet's response to different parameters
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- How to test that Akka actor was created in Scala
- Akka actors unit testing with Scala
- How to show images using Play framework and Scala in the view page
- Play Framework & Scala: Problems with FakeRequest & JSON POST in unit test
- How to mock an Akka Actor to Unit Test a class?
- How do you use play framework as a library, in a scala project
- How to test actions that expect an uploaded file in Play Framework (version 2.0, using Scala)?
- How to unit test an Akka actor that sends a message to itself, without using Thread.sleep
- How do I ignore ssl validation in play framework scala ws calls
- How do I unit test an akka Actor synchronously?
- How to list out all the files in the public/images directory in a Play Framework 2 Scala application?
- How to pass optional parameter to scala template in play framework 2
- How to import Play framework as a dependency in Scala project
More Query from same tag
- How to create xhtml query strings with Scala?
- What is the use of Universal trait in Scala?
- spark dataframe trim column and convert
- Can't run a Scala object in a Maven project in STS (Eclipse)
- Akka batch processing/one-item processing
- When, exactly, does Scala code perform heap allocations?
- Decimal Field Data Validation using Scala
- Efficient Scala idiomatic way to pick top 85 percent of sorted values?
- Splitting String with multiple spaces
- apache spark add column which is a complex calculation
- Transform a map into a new map based on the pattern of keys in Scala
- Gatling convert Json array to Map
- How to page REST calls in a future with Dispatch and Scala
- Immutable state in FP
- How to get specific key->value pair from a Map which is inside a List
- Is there anything wrong with asInstanceOf in this example using generics?
- IntelliJ IDEA 13 community edition with Scala
- Java IntelliJ error Cannot access for scala class
- pick up different source file for different Scala version
- SBT jetty webapp - files from src are not copied into target folder - can't view simple HTML files i placed there
- How do I get `sbt test` to recognize my custom target?
- Use list of String as a parameter in elastic4s termsQuery
- Json deserialization of Scala case objects with spray-json
- Understanding "type arguments do not conform to type parameter bounds" errors in Scala
- Achieving Ad hoc polymorphism at function parameter level (mixing parameters of different type)
- What does it mean to say that a method (such as 'react)' does not return?
- How to decrypt AWS KMS cipher with AWS Encryption SDK with Java or AWSKmsClient
- Escape string interpolation in anorm
- Spark Structured Streaming left outer joins returns outer nulls for already matched rows
- FS2 Stream with StateT[IO, _, _], periodically dumping state