Play Framework 2 Tutorial: Testing
How to test Play Framework routes, controllers and views
Published: 28 May 2012
This post, part three of the Play Framework tutorial series, I'm going to touch on the basics of testing. Three areas will be covered here:
- Testing Routes
- Testing Controllers
- Testing Views
This is by no means a comprehensive guide for testing but is aimed at folks just beginning to use the Play Framework. The Play Framework includes and uses JUnit by default.
Running tests
To run tests just run play test from your project root or simply test from the Play shell.
Testing Routes
package routes;
import org.junit.Test;
import play.mvc.Result;
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.GET;
import static play.test.Helpers.fakeRequest;
import static play.test.Helpers.routeAndCall;
public class IndexRouteTest {
@Test
public void rootRoute() {
Result result = routeAndCall(fakeRequest(GET, "/"));
assertThat(result).isNotNull();
}
@Test
public void badRoute() {
Result result = routeAndCall(fakeRequest(GET, "/bad"));
assertThat(result).isNull();
}
}
Testing Controllers
package controllers;
import org.junit.Test;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import play.libs.F;
import play.libs.WS;
import play.mvc.Result;
import play.test.TestBrowser;
import static org.fest.assertions.Assertions.assertThat;
import static play.mvc.Http.Status.OK;
import static play.test.Helpers.callAction;
import static play.test.Helpers.charset;
import static play.test.Helpers.contentAsString;
import static play.test.Helpers.contentType;
import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.running;
import static play.test.Helpers.status;
import static play.test.Helpers.testServer;
public class ApplicationControllerTest {
@Test
public void callIndex() {
Result result = callAction(controllers.routes.ref.Application.index());
assertThat(status(result)).isEqualTo(OK);
assertThat(contentType(result)).isEqualTo("text/html");
assertThat(charset(result)).isEqualTo("utf-8");
assertThat(contentAsString(result)).contains("hello, world");
}
@Test
public void testIndexWithFakeApp() {
running(fakeApplication(), new Runnable() {
@Override
public void run() {
// todo test model finder
}
});
}
@Test
public void testIndexWithTestServerRunnable() {
running(testServer(3333), new Runnable() {
@Override
public void run() {
assertThat(
WS.url("http://localhost:3333").get().get().getStatus()
).isEqualTo(OK);
}
});
}
@Test
public void runInBrowser() {
running(testServer(3333), HtmlUnitDriver.class, new F.Callback() {
public void invoke(TestBrowser browser) {
browser.goTo("http://localhost:3333");
assertThat(browser.$("body").getTexts().get(0)).isEqualTo("hello, world");
}
});
}
}
Testing Views
package views;
import org.junit.Test;
import play.mvc.Content;
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.contentAsString;
import static play.test.Helpers.contentType;
public class IndexViewTest {
@Test
public void indexTemplate() {
Content html = views.html.index.render("test");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(contentAsString(html)).contains("test");
}
}
Play Framework Jobs
jobs by 
