Skip to content

Commit

Permalink
Merge pull request playframework#281 from revbingo/lighthouse-954-patch
Browse files Browse the repository at this point in the history
[playframework#954] Enhance FunctionalTest to give access to renderArgs
  • Loading branch information
pepite committed Aug 8, 2011
2 parents d708ac0 + 72acb5f commit 2279032
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion documentation/manual/test.textile
Expand Up @@ -32,7 +32,7 @@ public class MyTest extends UnitTest {


h3. Functional test h3. Functional test


A functional test is written using JUnit. In this kind of test you can test your application by accessing directly the controller objects. A functional test is written using JUnit. In this kind of test you can test your application by accessing directly the controller objects.


Here is an example of a Functional test: Here is an example of a Functional test:


Expand All @@ -51,6 +51,16 @@ public class ApplicationTest extends FunctionalTest {


} }


Using the @renderArgs()@ method, you can also get direct access to the arguments passed to the view, instead of having to make assertions about the response itself. For example:

bc. @Test
public void testUserIsFoundAndPassedToView() {
Response response = POST("/user/find?name=mark&dob=18011977")
assertThat(renderArgs("user"), is(notNullValue());
User user = (User) renderArgs("user");
assertThat(user.name, is("mark"));
}

h3. Selenium test h3. Selenium test


Acceptance tests are written using Selenium. Here you can test your application by running it in an automated browser. Acceptance tests are written using Selenium. Here you can test your application by running it in an automated browser.
Expand Down
14 changes: 13 additions & 1 deletion framework/src/play/test/FunctionalTest.java
Expand Up @@ -23,6 +23,7 @@
import play.mvc.Http; import play.mvc.Http;
import play.mvc.Http.Request; import play.mvc.Http.Request;
import play.mvc.Http.Response; import play.mvc.Http.Response;
import play.mvc.Scope.RenderArgs;


import com.ning.http.multipart.FilePart; import com.ning.http.multipart.FilePart;
import com.ning.http.multipart.MultipartRequestEntity; import com.ning.http.multipart.MultipartRequestEntity;
Expand All @@ -44,6 +45,8 @@ public abstract class FunctionalTest extends BaseTest {


private static Map<String, Http.Cookie> savedCookies; // cookies stored between calls private static Map<String, Http.Cookie> savedCookies; // cookies stored between calls


private static Map<String, Object> renderArgs = new HashMap<String, Object>();

@Before @Before
public void clearCookies(){ public void clearCookies(){
savedCookies = null; savedCookies = null;
Expand Down Expand Up @@ -261,8 +264,13 @@ public static void makeRequest(final Request request, final Response response) {
final Future invocationResult = TestEngine.functionalTestsExecutor.submit(new Invoker.Invocation() { final Future invocationResult = TestEngine.functionalTestsExecutor.submit(new Invoker.Invocation() {


@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
renderArgs.clear();
ActionInvoker.invoke(request, response); ActionInvoker.invoke(request, response);

if(RenderArgs.current().data != null) {
renderArgs.putAll(RenderArgs.current().data);
}
} }


@Override @Override
Expand Down Expand Up @@ -428,6 +436,10 @@ public static String getContent(Response response) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }

public static Object renderArgs(String name) {
return renderArgs.get(name);
}


// Utils // Utils


Expand Down
Expand Up @@ -87,5 +87,13 @@ public void usingRedirection() {
assertIsOk(response); assertIsOk(response);
} }


@Test
public void canGetRenderArgs() {
Response response = GET("/users/edit");
assertIsOk(response);
assertNotNull(renderArgs("u"));
User u = (User) renderArgs("u");
assertEquals("Guillaume", u.name);
}
} }


0 comments on commit 2279032

Please sign in to comment.