Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Play Framework essentials
Play Framework essentials

Play Framework essentials: An intuitive guide to creating easy-to-build scalable web applications using the Play framework

eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Play Framework essentials

Chapter 2. Persisting Data and Testing

In this chapter, you will see how you can write executable specifications for your web service and how Play can integrate mainstream data persistence technologies like RDMSes or document stores. More precisely, you will see how to perform the following:

  • Write and run unit tests
  • Simulate HTTP requests and inspect returned HTTP responses
  • Persist data using an RDBMS
  • Use an in-memory database for development

Testing your web service

The architecture of your web service is depicted in the following diagram:

Testing your web service

This section presents the testing libraries that are integrated with Play and the testing infrastructure provided by Play to test the HTTP layer of your web service.

Writing and running tests

As Play projects are just sbt projects by default, you can add tests to your Play project just as you would do for any other sbt project, except that the root directory for test sources is not src/test/scala/ but simply test/.

sbt provides a mechanism to integrate testing libraries so that their tests can be run from the build system. The testing component of Play integrates two testing libraries out of the box: specs2 for Scala tests and JUnit for Java tests. Play projects automatically depend on the Play testing component, so you don't need to add this dependency in your build.sbt file. Obviously, you are free to use any other testing library supported by sbt—just follow their usage instructions...

Persisting data

Obviously, you are free to use any persistence technology such as relational databases, document stores, key-value stores, and graph databases according to your needs. Nevertheless, this section gives insights on the recommended ways to integrate a persistence technology to your Play project.

As the persistence layer is usually independent of the HTTP layer, Play is agnostic to which persistence layer you use. However, most persistence layers need configuration settings, so you need a way to inject these settings to your persistence technology from your application. A common way to achieve this is to define a Play plugin. The next sections show how to integrate a relational database using the provided JDBC and JPA plugins.

Using a relational database

If you are using a relational database, chances are that your database API relies on JDBC. At some point, your database API will need a javax.sql.Connection object to work with and might use JDBC transactions.

Play comes with a...

Using an in-memory database for tests

You now have all the required knowledge to make your web service persistent. Once you have chosen a database technology, select the Play plugins you need (JDBC, JPA, and so on), configure your data sources right from your application.conf file, use the database helper classes provided by the plugins to integrate with your database technology, and finally implement your business layer on top of your database layer.

Once you have made your business layer persistent, you probably want to run your tests to check whether you introduced a regression. However, you will unexpectedly get the following error:

[info] A Shop should
[info] ! add items
[error]    RuntimeException: : There is no started application  (Play.scala:71)

What happened? Remember that your database configuration is read by your Play application at startup and then used by your database layer. The key step is when you retrieve the data source—remember that the method takes an implicit...

Summary

The HTTP layer is made of the routes and controllers.Items components. The business layer is now made of two components, models.Shop and db.Schema (in Java, the latter component is hidden by JPA).

In this chapter, you saw how to write testing specifications for your web service, how to call your actions, supply them with fake HTTP requests, and process their results. You also saw how to integrate with relational database technologies. You learned that you can use the Play singleton to get a reference to the currently running application and, for instance, read its configuration settings. You learned how to hook into your application's life cycle using a global object. Finally, you saw how to start a fake application in your tests.

In the next chapter, you will see how to serve HTML pages and handle forms; your web service will evolve into a web application.

Left arrow icon Right arrow icon

Description

This book targets Java and Scala developers who already have some experience in web development and who want to master Play framework quickly and efficiently. This book assumes you have a good level of knowledge and understanding of efficient Java and Scala code.

What you will learn

  • Set up a unified development environment for both the clientside and serverside code
  • Understand the challenges of building a scalable web application and master the solutions provided by Play framework
  • Integrate the framework with existing clientside or serverside technologies such as persistence systems
  • Harness the reactive programming model to process data streams
  • Design robust, maintainable, and testable code
  • Be proficient in manipulating JSON data blobs
  • Deploy your application on a PaaS platform

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 25, 2014
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781783982417
Vendor :
Apache
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 25, 2014
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781783982417
Vendor :
Apache
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 66.98
Mastering play framework for scala
€41.99
Play Framework essentials
€24.99
Total 66.98 Stars icon

Table of Contents

8 Chapters
1. Building a Web Service Chevron down icon Chevron up icon
2. Persisting Data and Testing Chevron down icon Chevron up icon
3. Turning a Web Service into a Web Application Chevron down icon Chevron up icon
4. Integrating with Client-side Technologies Chevron down icon Chevron up icon
5. Reactively Handling Long-running Requests Chevron down icon Chevron up icon
6. Leveraging the Play Stack – Security, Internationalization, Cache, and the HTTP Client Chevron down icon Chevron up icon
7. Scaling Your Codebase and Deploying Your Application Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6
(8 Ratings)
5 star 25%
4 star 50%
3 star 0%
2 star 12.5%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




Mike Feb 02, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent book with a lot of examples and tips. Provides a continuous learning form the beginning in a medium technical language which helps understanding everything, even if we are new at PLAY.Recomend it for sure. For deep knowledge the Cookbook is also a mandatory book to summarize, review and learn some new tricks.
Amazon Verified review Amazon
Abhishek Srivastava Jun 24, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I love this book. I tried couple of play framework books on safari. I found that either they were were old and examples were obsolete or they were very poorly written.Finally I found this book and it is very recent. all examples work. nothing is obsolete.I also find that the book is comprehensive and well written.
Amazon Verified review Amazon
Martin Nov 24, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
- Concise reading without long stories.- Short code snippets cover just the important parts of solution.- Includes everything important like session authentication, CSRF prevention and others. (I have not found these topics in few other books.)
Amazon Verified review Amazon
Juraj Zachar Jan 01, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The content of the book is great but the kindle format is hard to work with. The lack of navigable list of contents makes browsing of the book's chapters cumbersome. I was expecting much better usability and I wish I had gone for a hard cover.
Amazon Verified review Amazon
Dustin Marx Nov 01, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
"Play Framework Essentials" provides a rapid introduction to the Play Framework. In 7 chapters and under 200 pages, "Play Framework Essentials" introduces Play Framework and covers details such as building and exposing web services with Play, building web application clients that integrate with Play, persisting data from a Play application to a relational database, testing a Play application, refactoring Play application code, and deploying a Play application. The book also describes how developers can take advantage of Play's concurrency model, security support, internationalization support, and caching support."Play Framework Essentials" provides numerous code listings. These are black font on white background with no line numbers and no color syntax. The examples demonstrate applying Play in both Java and Scala programming languages and explains the relatively small number of features that are only available in one language or the other. There are also several graphics in the book that are mostly grayscale but one or two of them has some color in it.Although there are fewer than 200 pages of substantive text in "Play Framework Essentials," the book still manages to cover a significant amount of technical detail. This is accomplished by spending very little time covering historical or background details of the Play Framework and jumping right into technical discussion.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.