Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing your Play! controllers

DZone's Guide to

Testing your Play! controllers

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Lets say you have a controller like this (from the Play! documentation)

package controllers;
  
import java.util.*;
  
import play.*;
import play.mvc.*;
  
import models.*;
  
public class Application extends Controller {
  
    public static void index() {
        Post frontPost = Post.find("order by postedAt desc").first();
        List<post> olderPosts = Post.find(
            "order by postedAt desc"
        ).from(1).fetch(10);
        render(frontPost, olderPosts);
    }
  
}

And you want to write a test to check the render parameters are correct, how do you do that?

Well here is a little example
public class ApplicationTest extends FunctionalTest{
 @Before
 public void before() {
  Fixtures.deleteAllModels();
  Fixtures.load("data.yml");
 }
 
 @Test
 public void testHomePageFirstPost() throws MalformedURLException {
  // Call the home page
  Response response = GET("/");
   
  // Verify that Matches.matchCreate() passed correct params to render().
  Post frontPost = (Post) getRenderParameter("frontPost");
  assertEquals(${value_from_yml}, frontPost.author.fullname);
  assertEquals(${value_from_yml}, frontPost.postedAt);
 }
 
 /**
  * Gets the parameter that was passed to render() method.
  * @param key
  * @return
  */
 public static Object getRenderParameter(String key) {
  RenderArgs current = Scope.RenderArgs.current();
  return current.data.get(key);
 }
 
}

You might want to put the getRenderParameter in a separate class to easily reuse it.


If you want to test a controller method that take parameters is as easy.

Let's assume you want to test a simple controller method in Users called getUserByUsername to make sure the correct user is added to render when the form is submitted

public class Users extends Controller {
  
 /**
 * Gets the user given his username
 */
    public static void getUserByUsername(String username) {
        Assert.notNull(user);
        User currentUser = User.find("byUsername", username).first();
        render(currentUser);
    }
  
}


In this case the test would be

public class UserLoginTest extends FunctionalTest{
 
 @Before
 public void before() {
  Fixtures.deleteAllModels();
  Fixtures.load("users.yml");
 }
  
 @Test
 public void testGetByUsername() {
  Map<String, String> params = new HashMap<String, String>();
  params.put("username", "user1");
  Response response = POST("/users/getbyusername", params, new HashMap<String, File>());
  User currentUser = (User) getRenderParameter("currentUser");
  assertEquals(${from_users.yml}, currentUser.name);
  assertEquals(${from_users.yml}, currentUser.email);
 }
}

Please feel free to comment!
 
From http://www.devinprogress.info/2011/03/unit-test-your-play-controllers.html

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}