You are viewing the documentation for the 2.1.x release series. The latest stable release series is 2.7.x.

§Writing functional tests

§Testing a template

As a template is a standard Scala function, you can execute it from a test and check the result:

public void renderTemplate() {
  Content html = views.html.index.render("Coco");

You can find the complete list of the test helpers in the Helper class API documentation.

§Testing your controllers

You can also retrieve an action reference from the reverse router, such as controllers.routes.ref.Application.index. You can then invoke it:

public void callIndex() {
    Result result = callAction(
    assertThat(contentAsString(result)).contains("Hello Kiki");

§Testing the router

Instead of calling the Action yourself, you can let the Router do it:

public void badRoute() {
  Result result = routeAndCall(fakeRequest(GET, "/xx/Kiki"));

§Starting a real HTTP server

Sometimes you want to test the real HTTP stack from with your test. You can do this by starting a test server:

public void testInServer() {
  running(testServer(3333), new Runnable() {
      public void run() {

§Testing from within a web browser

If you want to test your application from with a Web browser, you can use Selenium WebDriver. Play will start the WebDriver for your, and wrap it in the convenient API provided by FluentLenium.

public void runInBrowser() {
    running(testServer(3333), HTMLUNIT, new Callback<TestBrowser>() {
        public void invoke(TestBrowser browser) {
           assertThat(browser.$("#title").getTexts().get(0)).isEqualTo("Hello Guest");
           assertThat(browser.$("#title", 0).getText()).isEqualTo("Hello Coco");

Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.