forked from playframework/playframework
-
Notifications
You must be signed in to change notification settings - Fork 3
JavaTest
ndeverge edited this page Jun 11, 2012
·
8 revisions
Test source files must be placed in your application’s test
folder. You can run tests from the Play console using the test
(run all tests) and test-only
(run one test class: test-only my.namespace.MySpec
) tasks.
The default way to test a Play 2 application is with JUnit.
package test;
import org.junit.*;
import play.mvc.*;
import play.test.*;
import play.libs.F.*;
import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;
public class SimpleTest {
@Test
public void simpleCheck() {
int a = 1 + 1;
assertThat(a).isEqualTo(2);
}
}
If the code you want to test depends on a running application, you can easily create a FakeApplication
on the fly:
@Test
public void findById() {
running(fakeApplication(), new Runnable() {
public void run() {
Computer macintosh = Computer.find.byId(21l);
assertThat(macintosh.name).isEqualTo("Macintosh");
assertThat(formatted(macintosh.introduced)).isEqualTo("1984-01-24");
}
});
}
You can also pass (or override) additional application configuration, or mock any plugin. For example to create a FakeApplication
using a default
in-memory database:
fakeApplication(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