Specs2

Specs2 is a library for writing executable software specifications. With specs2 you can write software specifications for one class (unit specifications) or a full system (acceptance specifications).

Dependency

// for javax
"org.scalatra" %% "scalatra-specs2-javax" % "3.0.0" % "test"

// for jakarta
"org.scalatra" %% "scalatra-specs2-jakarta" % "3.0.0" % "test"

Selecting a testing style

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

Specs2 testing style trait
MutableScalatraSpec org.specs2.mutable.Specification (Unit specification)
ScalatraSpec org.specs2.Specification (Acceptance specification)

Examples

Unit testing

From the Specs2 Structure:

you can create a “Unit” specification where the code is interleaved with the text. The name “unit” comes from the fact that Unit specifications have a structure which is close to unit tests in “classical” frameworks such as JUnit

import org.scalatra.test.specs2._

class HelloWorldMutableServletSpec extends MutableScalatraSpec {
  addServlet(classOf[HelloWorldServlet], "/*")

  "GET / on HelloWorldServlet" >> {
    "must return status 200" >> {
      get("/") {
        status must_== 200
      }
    }
  }
}
Acceptance testing

From the Specs2 Structure:

you can create an “Acceptance” specification where all the informal text is written in one place and the code is written somewhere else. The name “acceptance” comes from the fact that it might be easier for a non-developer to just read some text to validate your specification

import org.scalatra.test.specs2._

class HelloWorldServletSpec extends ScalatraSpec { def is = s2"""
  GET / on HelloWorldServlet
    must return status 200           $getRoot200
"""

  addServlet(classOf[HelloWorldServlet], "/*")

  def getRoot200 = get("/") {
    status must_== 200
  }
}