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 models


The following recipe focuses on writing a test for our model objects. We will create a new record and add assertions to validate the object's creation. We will then use the Activator command to run our test.

How to do it...

For Java, we need to take the following steps:

  1. Edit the ProductTest.java file and add the following content:

        // add new imports
        import static play.test.Helpers.*;
        import models.*;
        import play.test.*;
    
        // add new test
        @Test
        public void testSavingAProduct() {
          running(fakeApplication(), new Runnable() {
            public void run() {
              
         Product product = new Product();
              product.name = "Apple";
              product.save();
              assertNotNull(product.getId());
            }
          });
        }
  2. Execute the new spec by running the command test-only ProductTest:

        [foo_java] $ test-only ProductTest
        [info] Passed: Total 2, Failed 0, Errors 0, Passed 2
        [success] Total time: 2 s, completed 09 29, 14 9:33:43 PM
    

For Scala, we need to take the following steps:

  1. Edit the ProductSpec.scala file and add the following content:

        import models._
        import play.api.test.WithApplication
    
        "models.Product" should {
          "create a product with save()" in new WithApplication {
                    val product = Product(1, "Apple")
            val productId = Product.save(product)
    
              productId must not be None
          }
        }
  2. Execute the new spec by running the command test-only ProductSpec:

        [foo_scala] $ test-only ProductSpec
        [info] Compiling 1 Scala source to /private/tmp/foo_scala/target/scala-2.11/test-classes...
        [info] ProductSpec
        [info]
        [info] The 'product' string should
        [info] + contain seven characters
        [info]
        [info] models.Product should
        [info] + create a product with save()
        [info]
        [info] Total for specification ProductSpec
        [info] Finished in 1 second, 90 ms
        [info] 2 examples, 0 failure, 0 error
        [info] Passed: Total 2, Failed 0, Errors 0, Passed 2
        [success] Total time: 4 s, completed 09 29, 14 4:28:51 PM
    

How it works...

In this recipe, we added a new spec, where we created a new product and invoked the save() method. We then added assertion statements to validate that the value returned by the save() method is not equal to none. The test command is used to run the test and displays the results of the test.