Play 2.0 with Scala and Scaml, Part1: Setup of test infrastructure, model and persistence with Anorm

In this series I’ll try to provide some help on topics I had problems with when I implemented my first Play 2.0 project. My goal is it to get a small version of the blogging engine of the 1.0 tutorial. I will not provide a complete tutorial, but only stuff that might be helpfull setting up the project.

Hint for all beginners with the 2.0 RC: One source of knowledge I was missing in the beginning is the 2.0 wiki, which is often enough quite useful.

Setting up IntelliJ

I setup IntelliJ as described in here. Everything worked except the server startup. To have it started you have to use a different main class: play.core.server.NettyServer. Server startup works like this, but the application is not loaded correctly. As this has no priority for me, I didn’t spend much time trying to solve this. If anyone knows what the problem is, please let me know.

The first model

In the tutorial three classes were defined: User, Post and Comment. From my perspective Post and User should be enough to cover all the basic topics and problems, so I’ll skip the comment. The definition of User and Post in scala is pretty easy. The Pk herein is needed as there is foreign key relationship between the tables as you’ll see later. It’s an optional parameters and thus most users will hardly ever notice that it’s there.

case class User(email: String, password: String, fullname: String, isAdmin: Boolean = false, id:Pk[Int] = NotAssigned)

case class Post(title: String,  posted: Date, content: String, author: User)


To persist the entities we’ll need some persistence. I will use the standard h2 in memory db. If the data is read and written mainly in whole entities by a PK, like here,  I’d normally prefer a NoSQL-DB like Mongo or Neo4J. But for the sake of simplicity I’ll stick with the standard DB here.

Play is using the concept of Evolutions for DBs. The basic idea of evolutions is to describe what must be executed to perform the evolution (Ups) and what must be done to undo the evolution (Downs). This is probably not the best and golden way, but a pretty good one especially for smaller projects. For a good wrap up about problems and solutions of DB changes have a look here.

For Post and User the first evolution might look like this and has to be defined in [PROJECT_HOME]/conf/evolutions/default/1.sql:

# --- First database schema
# --- !Ups
id                            INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
email                         VARCHAR(255) NOT NULL,
password                      VARCHAR(255) NOT NULL,
fullname                      VARCHAR(255) ,
isAdmin                       BOOLEAN NOT NULL
title                         VARCHAR(255) NOT NULL,
posted                        DATE NOT NULL,
content                       VARCHAR(2550) NOT NULL,
authorId                      INT NOT NULL,
FOREIGN KEY                   (authorId) REFERENCES myuser(id)

# --- !Downs

Implementing the persistence test driven with Anorm

In the standard tutorial the test mode is described, where one can open the @test url and see all tests. I haven’t found this in 2.0 yet. Right now I don’t bother too much, as I’ll run the tests from within the IDE. Anyway: If anyone knows how to get the web based tests running again, gimme a note.

I like to write test first. The implementation of integration tests for the persistence differs from version 1. Thus I’ll explain it in a little more detail. My first test for the persistence looked like this:

"The user class" should {
  "be persisted" in {
    running(FakeApplication()) {
      User.create(User("email", "pwd", null))
      User.findAll() must have size 1

I made some mistakes in the setup and needed quite a time to figure out what the problem was. One thing that really bothered me was, that the log of the test didn’t tell what the problem was. It just stated that the table MYUSER doesn’t exist. In my case there were 2 problems. First of all there was no evolution applied as I named the directory wrong. Second there was a syntax error in my SQL script. One of the real valuable benefits of test should be to get a hint where an error is located. To achieve this I wrote another test, that explicitly tested the application of the evolutions:

class DBEvolutionsTest extends Specification {

  "Evolutions" should {
    "be applied without errors" in {
      running(FakeApplication()) {
        DB.withConnection {
          implicit connection =>
            SQL("select count(1) from myuser").execute()
            SQL("select count(1) from posts").execute()

What happens here? First we tell the testing Helpers class that it should apply the evolutions for the DB default. The connection for this DB is defined in the application.conf (db.default.driver etc.) and the evolution as already described in the 1.sql, where the db name default is defined in the path. Then a fake application is created and a simple SQL is executed to check, that the table exists.

Written like this, logs and error messages have the verbosity I prefer. You can now see in your test log how the evolution is applied. If there is a syntax error it’ll be shown. If you haven’t put the evolution at the right place (as I did) you will at least see, that the evolution is not applied.

You might have noticed that I have one nullable column in my schema definition. Normally I’m not a great friend of null values and try to avoid them wherever possible. In this case I’d definitely make it not nullable too. But to have examples of all the “standard” cases, I thought it would be a good idea to have at least one nullable column in the example.

I’ll skip the rest of the tests as they are pretty straight forward from here on. The resulting User object used for persistence looked like this in the end. I used the SqlParser of Anorm to create a parser that could be used in the data access methods later on. As you can see here, the nullable column is PITA again. You’ll have to access it via an option and have to provide a value in case there is none given. Otherwise there will be an UnexpectedNullableFound exception be thrown. To be “consistent” I’ve choosen null to be set in User again .. argh.

The Pk of a new entity is set after insertion of a new User via SELECT SCOPE_IDENTITY(). I’m not sure if this is the way to go and if it is thread safe, but it works for this little example at least. The Pk definition is needed for the persistence of the posts again, as we have a foreign key relationship between the two.

object User  {
  val parser = {
      get[Pk[Int]]("id") ~
      get[String]("email") ~
      get[Boolean]("isAdmin") map {
      case pk ~ mail ~ name ~ fullname ~ isAdmin => User(mail, name, fullname.getOrElse(null), isAdmin, pk)

  def findBy(id: Pk[Int]): User = {
    DB.withConnection {
      implicit connection =>
        SQL("select * from myuser where id = {id}").on("id" -> id.get).using(parser).single()

  def findAll(): Seq[User] = {
    DB.withConnection {
      implicit connection =>
        SQL("select * from myuser").as(User.parser *)

  def create(user: User): User = {
    DB.withConnection {
      implicit connection =>
        SQL("insert into myuser(email, password, fullname, isadmin) values ({email}, {password}, {fullname}, {isAdmin});").on(
          'email ->,
          'password -> user.password,
          'fullname -> user.fullname,
          'isAdmin -> user.isAdmin
        val id = SQL("SELECT SCOPE_IDENTITY()")().collect {
          case Row(id: Int) => id
        return User(, user.password, user.fullname, user.isAdmin, new Id(id))

In the following definition of the post object you can see the why the Pk of the user is needed after creation.

object Post{
  val parser = {
      get[String]("title") ~
      get[Date]("posted") ~
      get[Pk[Int]]("authorId") map {
      case title ~ posted ~ content ~ author => Post(title,  posted, content, User.findBy(author))

  def findAll() = {
    DB.withConnection {
      implicit connection =>
        SQL("select * from posts").as(parser *)

  def create(post: Post): Unit = {
    DB.withConnection {
      implicit connection =>
        SQL("insert into posts(title, posted, content, authorId) values ({title}, {posted}, {content}, {authorId})").on(
          'title -> post.title,
          'posted -> post.posted,
          'content -> post.content,
          'authorId ->


Setup of the project and integration in the IDE worked like a charm. Same holds for setting up the test infrastructure if you have found where its documented. The docs will be made more visible when 2.0 is officially released, so no problem. I like the verbosity of the test helpers. It becomes really obvious what is happening, without making the tests too chatty. I really love the basic idea about the Anorm framework for database access. It’s clear and concise and again it becomes clear in the code what is happening. I’m not sure what I think about the lack of the Magic trait. Having to write words like password 7 times is a major PITA for me. I’ definitely love seeing it again in one of the upcoming versions.

ScalaTest with IntelliJ

I just had a couple of problems with IntelliJ and ScalaTest. Under some circumstances, some of the tests didn’t run anymore. I had the problems some times before, but didn’t find anything helpful in the net, yet. I was really going crazy about this, ’cause nothing seems to help. Well I managed to get around it. Sadly without knowing exactly what the problem was. I just creating a completely new project in IntelliJ (project file format .idea (standard) and ‘Java’ not ‘Plugin‘..) copied the sources over to the new one, re-init of maven, and now it seems to work.

I played around with the project structure before the re-init. Might have caused some problems. Hoping everything stays fine now.

Choosing the IDE(A)

For me using an IDE is essential to work efficient. Remembering the time when the IDE’s were better editors is like thinking of my first time: It was feasible, but not fun, exhausting and I thought it was OK. Nowadays I know better: It is really fun if you know how and have the right one.

First of all let me say which features of an IDE are most important to me, then I’ll give you a highly opinionated overview why I’ve chosen the IDEA.

A good auto-completion also showing docs

  • Smooth navigation through the code and files
  • Good integration of third-party tools (in my case Maven and ScalaTest)
  • Overall ease of use and install

1. NetBeans

I started developing Scala with NetBeans (v6.8). Auto-completion wasn’t a dream, but I could use the environment I do best known and use Maven for builds. It started getting a little awkward when I started using ScalaTest. Well it was working. There was a good test runner from ScalaTest itself and how to include it without using Maven ist described here: Furthermore there is a JUnit test runner for ScalaTest, that can be used within NB with an (ugly) annotation.

Then NetBeans 6.9 came out. There were 2 Problems with 6.9:

  1. I got used to it quickly using it in my company and the Java parts I was working on in my private project.
  2. The Scala Plugin didn’t work in the first place.

So I started looking for an alternative…

2. Scala IDE (Eclipse)

Next I gave the Scala IDE (Eclipse) a try. Name is promising, isn’t it? Well I was pretty disappointed quickly. Good things first: Installation is easy. Auto-completion is quite good and navigation was OK too. For the cons an own paragraph:

The IDE comes with an own distribution of Scala and – for me at least – it was not straight forward to change it to the actual one. Furthermore I had several bugs with the run configurations. No real showstopper but things that are annoying when coming back again and again. As I want to use Maven at some point I had a look in the Manuals for the ability the Plugin to use Maven. Well to keep it short, its possible but not as easy as I like it.

One more – for me important – thing:

The look and feel. I don’t like the eclipse one any more. Its kind of ugly and bumpy. For me NetBeans is still the nicest and cleanest IDE. IntelliJ is OK, but Eclipse. Sorry guys. I know you are doing a great job providing this Framework for free for years now. But if you ask me: IT NEEDS A MAJOR FACELIFT….

3. IntelliJ IDEA

At that time I was starting getting annoyed by Eclipse, the JetBrain guys decided to free and open source a community edition of IntelliJ (thank you so much guys). Some of my colleagues were using it already and they have been quite impressed. So I thought: Let’s have a look if the community edition has a Scala feature too. Well, it has! And it’s great! Easy to install, use. The Maven support is good. Not as nice as it is in NetBeans. But auto-completion for the POM is working well, that’s probably most important.

Where there’s light there is shadow. Here are the cons:

  • annoying, f…ing long compile time (c.f. paragraph below)
  • often unable to infer the type of a variable for auto completion of  members

A good how to you’ll find here:

How to reduce this annoying compile time in the IDEA

Really simple if you know how. Make a new run config “Scala Compile Server”, give it a name e.g. “the magic server” or something you like, uncheck the make option (mui importante), and then run, baby, run…

Dunno if it really works. It sometime exits with some error exit code. But compilation overall feels faster now. And nothing else matters… 😉

BTW I’m working on OS X.