Test Module for Play!Framework

The Test Module for Play!Framework helps you write tests quicker, is a cleaner and reusable manner.

Play provides an environment for testing. However that environment is full of hard-coded strings, and the code you write is neither reusable nor maintainable.

When you start a new Play application, Play generates a sample code for you that looks like this :


Response response = GET("/");
 assertIsOk(response);
 assertContentType("text/html", response);
 assertCharset("utf-8", response);

I have several problems with this code.

  • The path "/" is unclear.. In order to know what you are testing, you need to check the "routes" file.

  • Calling actions with path string makes tests hard to maintain and develop. If you have a signature with many parameters (for example, signup form or survey form) it will be hard to write the path. If I will change an argument's name in the controller's signature I will need to modify all related path strings.

  • Why am I specifying "GET"? Why am I not specifying the method I want to call, and let Play figure it out by itself? As a developer it's natural to me to call a method instead of constructing a URL.

  • It is not Object oriented enough, and hence the code is less reusable. I would probably want to check that the response is OK many times. maybe even on every response. If the code was OO I could override "GET" and add a check that the response is ok.

  • Strings are hard-coded. For example "text/html" and "utf-8". This is error prone.

  • Play already does all the things I specify. So why not in the tests?


  • My Wishes

    I wish I could simply call the controller's function.

    In the example above, the path "/" will direct me to controller "Application" and action "index". I wish I could simply call Application.index() and that's it. However, for someone outside of Play it will be hard to implement if not impossible.

    The reason for it being so hard lies in the instrumentation technic which is massively used all around Play and the fact that the methods are static.

    I will not elaborate this topic here, nor will I talk about why Play chose to implement the actions in static methods, which are not OO by nature. I believe that Singletons would've made a much better choice.



    Due to all these difficulties, I will settle for the following syntax
    
    ApplicationTestController c = new ApplicationTestController();
    c.index().isOk().is(UTF8).is(HTML);
    

    My module allows you to have similar syntax to this.

    In this syntax I know exactly which function is called. I don't care which Path is used, and definitely not which method (GET/POST).

    I know which arguments to use with a simple "auto complete". If I change the signature of the controller I can simply refactor my "TestApplicationController" to modify the test easily accordingly.

    Note I am not mentioning Response anywhere.. I have less code, HTML and UTF8 are Enums, so I know exactly what my options are.

    Getting Started

    All the info and tutorials you need can be found by code samples, and wiki pages available to you in GitHub (link below).

    Since this little (10 classes.. ) project of mine combines cool technologies like Play and CGLib, I will write a series of posts about how I wrote it. (links below).


    References

  • Project on GitHub


  •