ScalaTest

ScalaTest supports three main styles of testing out of the box: test-driven development (TDD), behavior-driven development (BDD), and acceptance testing. ScalaTest also supports writing JUnit and TestNG tests in Scala.

Dependency

"org.scalatra" %% "scalatra-scalatest" % "2.7.0" % "test"

Selecting a testing style

A trait of Scalatra integration is prepared according to the testing style of ScalaTest. You can select a trait according to the testing style you want to use.

ScalaTest testing style trait
FunSpec ScalatraSpec
FlatSpec ScalatraFlatSpec
FreeSpec ScalatraFreeSpec
WordSpec ScalatraWordSpec
FunSuite ScalatraFunSuite
FeatureSpec ScalatraFeatureSpec
JUnit3Suite ScalatraJUnit3Suite
JUnitSuite (JUnit 4) ScalatraJUnitSuite
TestNGSuite ScalatraTestNGSuite

At this time, traits for PropSpec and RefSpec are not prepared.

Example

When creating a test class, extend Scalatra’s trait according to the testing style of ScalaTest. At this time, ScalaTest’s Matcher trait is already in effect, so you do not need to mix-in.

The following code shows an example of code when FunSuite is selected as the testing style.

import org.scalatra.test.scalatest._

class HelloWorldServletTests extends ScalatraFunSuite {
  // `HelloWorldServlet` is your app which extends ScalatraServlet
  addServlet(classOf[HelloWorldServlet], "/*")

  test("simple get") {
    get("/path/to/something") {
      status should equal (200)
      body should include ("hi!")
    }
  }
}