forked from playframework/playframework
-
Notifications
You must be signed in to change notification settings - Fork 0
ScalaTest
Guillaume Bort edited this page Feb 4, 2012
·
12 revisions
Tests source files must be placed in the test folder of your application. You can run them from the play console using the test and test-only tasks.
The default way to test a Play 2 application is by using specs2.
Unit specifications extend the org.specs2.mutable.Specification trait and are using the should/in format:
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
}
If the code you want to test depends of a running application, you can easily create a FakeApplication on the fly:
"Computer model" should {
"be retrieved by id" in {
running(FakeApplication()) {
val Some(macintosh) = Computer.findById(21)
macintosh.name must equalTo("Macintosh")
macintosh.introduced must beSome.which(dateIs(_, "1984-01-24"))
}
}
}
You can also pass (or override) additional configuration to the fake application, or mock any plugin. For example to create a FakeApplication using a default in memory database:
FakeApplication(additionalConfiguration = inMemoryDatabase())
Next: Writing functional tests
- HTTP programming
- Asynchronous HTTP programming
- The template engine
- HTTP form submission and validation
- Working with JSON
- Working with XML
- Handling file upload
- Accessing an SQL database
- Using the Cache
- Calling WebServices
- Integrating with Akka
- Internationalization
- The application Global object
- Testing your application