Book Image

Play Framework Cookbook - Second Edition

By : Alexander Reelsen, Giancarlo Inductivo
Book Image

Play Framework Cookbook - Second Edition

By: Alexander Reelsen, Giancarlo Inductivo

Overview of this book

<p>As web and mobile systems become more sophisticated, anchoring systems in a mature, solid framework has become increasingly important. Play 2 provides developers with the necessary tools to build robust web applications.</p> <p>This book is a compilation of useful recipes aimed at helping developers discover the power of Play 2. The introductory section serves as a primer to Play Framework, wherein all the fundamentals of the framework are covered extensively. It then explains the usage of controllers and how modules can be leveraged for optimal performance. Next, the book walks you through creating and using APIs, followed by extensive real-world applications. Finally, you will learn to manage applications post production.</p>
Table of Contents (15 chapters)
Play Framework Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Testing with JUnit (Java) and specs2 (Scala)


It is quite important for a web framework to integrate testing as seamlessly as possible with the web framework itself. This minimizes the friction developers encounter when coding functional specs and writing tests to validate their work. For Play Java projects, we will be utilizing the popular test framework JUnit. We will be using it to do a simple unit test and to test our model and controller action. For Play Scala projects, we will be using specs2 to do a simple unit test and to test our model, a controller action, and a route mapping.

How to do it...

For Java, we need to take the following steps:

  1. Create a new spec class, ProductTest.java, in test/ and add the following content:

        import static org.junit.Assert.*;
        import org.junit.Test;
    
        public class ProductTest {
        
          @Test
          public void testString() {
            String str = "product";
            assertEquals(7, str.length());
          }
        }
  2. Run the first spec using Activator by running the command test-only ProductTest:

        $ activator
        [info] Loading project definition from /private/tmp/foo_java/project
        [info] Set current project to foo_java (in build file:/private/tmp/foo_java/)
        [foo_java] $ test-only ProductTest
        [info] Compiling 3 Java sources to /private/tmp/foo_java/target/scala-2.11/test-classes...
        [info] Passed: Total 1, Failed 0, Errors 0, Passed 1
        [success] Total time: 3 s, completed 09 29, 14 8:44:31 PM
    

For Scala, we need to take the following steps:

  1. Create a new Spec class, ProductSpec.scala, in test/ and add the following content:

        import org.specs2.mutable._
    
        class ProductSpec extends Specification {
    
          "The 'product' string" should {
            "contain seven characters" in {
              "product" must have size(7)
            }
          }
        }
  2. Run the first spec using Activator by running the command test-only ProductSpec:

        $ activator
        [info] Loading project definition from /private/tmp/foo_scala/project
        [info] Set current project to foo_scala (in build file:/private/tmp/foo_scala/)
        [foo_scala] $ test-only ProductSpec
        [info] ProductSpec
        [info]
        [info] The 'product' string should
        [info] + contain seven characters
        [info]
        [info] Total for specification ProductSpec
        [info] Finished in 24 ms
        [info] 1 example, 0 failure, 0 error
        [info] Passed: Total 1, Failed 0, Errors 0, Passed 1
        [success] Total time: 2 s, completed 09 29, 14 12:22:57 PM
    

How it works...

In this recipe, we created a brand new spec file that will contain our test specifications. We placed this file inside the test/ directory and ran the test using activator with the test-only command. The test command is used to run the test and it displays the results of the test.