A generic helper for database related tests with play 2 and JPA

Posted by Jens Jäger on December 31, 2013

All your database related unit tests for a play 2 java application have to handle the fakeApplication startup, load an fresh in memory database and load the test related fixtures. You need fixy and this helper to do the trick:

To use the helper you just have to extend from ModelTest and override the fixturesToLoad method:

The tasks.yaml loaded in fixturesToLoad must be placed in the conf/fixtures folder of the play application.

The source code is pushed to Play4Jpa repo on github.

Yaml-Fixtures for play 2 1

Posted by Jens Jäger on December 30, 2013

Fixtures is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run.

The fixture implementation for loading testdata in play 1 was great. But all fixture related code for play 2 I found was ebean releated. After some research I decided to use Fixy for loading test data. It needed only some small changes to adapt it for play 2.

I added the code as a module to play4jpa. To use it, just copy the module to your project and add the necessary config to your Build.scala.

A yaml fixture file in the play conf folder can be loaded inside a transaction with this code:

The yaml for a Task Model with a name an done flag would look like this:

Check out the samples in the fixy unit tests on github for more details on using fixy.

A Ebean like finder for JPA 1

Posted by Jens Jäger on December 29, 2013

After the decision to go for JPA instead of Ebean with a Play Framework project, the first problem to solve was a replacement for the ebean finder.

The project team started with these simple requirements:

  • Jpa based
  • Easy to use
  • Can return a finder object, to add restrictions outside of the model (Used for generic components like the sort function of a data table view)

After some prototyping with different implementations, we found this gist and decided to start from there with Hibernate Criteria.

You can find the results in the query package and the generic model of my Play4Jpa project. The GenericModel doesn’t contain a generated long based @Id. You should only extend from GenericModel when you use combined keys. If you need a long based @Id you can extend from Model.

To get a query object from a Object that extends Model. You just have to add the method:

To find all Tasks with the query you just need to add the following findAll Method:

Here is a example of a simple where condition:

Play 2.x WS API and self signed certificates 5

Posted by Jens Jäger on December 03, 2013

The play web service API provides an easy way to use your play application as an http client. Usually you should use Play’s asynchronous mechanisms to use the web service response. But sometime you just want to wait for the web service responce (in a job). You can do this with the following method:

When the web service you want to call uses self signed certificates you will run into an exception:

To allow self-signed ssl certificates you just have to add the following to your application.conf.

When you unit test your web service call make sure your test runs inside a FakeApplication.

Play framework conference

Posted by Jens Jäger on December 02, 2013

Just bought a ticket to Ping Play! Edition. A 2-day by-the-community and for-the-community conference about the Play Framework.

Anybody knows a good hotel near BMC?