Testing Scala and Play with Scala Test + Play

There are many ways to unit test your Scala and Play code. At the time of this writing (Scala 2.11.6) I prefer using Scala Test + Play. It’s pretty easy to use and seems to let me do all I need.

Setup Dependency

Open build.sbt and add these dependencies.

libraryDependencies ++= Seq(
  specs2 % Test,
  //Scala Test + Play
  "org.scalatest" %% "scalatest" % "2.2.1" % "test",
  "org.scalatestplus" %% "play" % "1.4.0-M3" % "test"

Write a Test Suite

Create a class that extends org.scalatestplus.play.PlaySpec.

import org.scalatestplus.play.PlaySpec

class MyTestSpec extends PlaySpec  {
  "Application" should {
    "Addition test" in {
      (2 + 2) mustBe 4
    "Multiply test" in {
      (2 * 4) mustBe 8

Running a Test Suite

To run all tests using sbt run:

sbt test

In IntelliJ right click the test suite class – MyTestSpec – and select Run MyTestSpec.

Override Application Settings

If your Play application uses settings from application.conf then you probably wish to override some of the settings just during unit testing. This can be done fairly easily. Let’s say that our application.conf has this:


During unit testing we will like money-server-host to point to localhost. To do this we have to setup a play.api.test.FakeApplication.

import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.Play._
import play.api.test.FakeApplication

class MyTestSpec extends PlaySpec with OneAppPerSuite {
  //Override test specific settings here.
  implicit override lazy val app: FakeApplication =
      additionalConfiguration = Map(
        "money-server-host" -> "localhost"

  "Application" should {
    "Settings test" in {
        mustBe "localhost"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s