Search code examples
scalaactiverecordplayframework-2.0cake-pattern

Integration tests in Scala when using compagnons with Play2? -> Cake pattern?


I'm working on my first Scala application, where we use an ActiveRecord style to retrieve data from MongoDB.

I have models like User and Category, which all have a companion object that uses the trait:

class MongoModel[T <: IdentifiableModel with CaseClass] extends ModelCompanion[T, ObjectId]

ModelCompanion is a Salat class which provide common MongoDB crud operations. This permits to retrieve data like this:

User.profile(userId)

I never had any experience with this ActiveRecord query style. But I know Rails people are using it. And I think I saw it on Play documentation (version 1.2?) to deal with JPA.

For now it works fine, but I want to be able to run integration tests on my MongoDB. I can run an "embedded" MongoDB with a library. The big deal is that my host/port configuration are actually kind of hardcoded on the MongoModel class which is extended by all the model companions.

I want to be able to specify a different host/port when I run integration tests (or any other "profile" I could create in the future).


I understand well dependency injection, using Spring for many years in Java, and the drawbacks of all this static stuff in my application. I saw that there is now a scala-friendly way to configure a Spring application, but I'm not sure using Spring is appropriate in Scala.

I have read some stuff about the Cake pattern and it seems to do what I want, being some kind of typesafe, compile-time-checked spring context. Should I definitely go to the Cake pattern, or is there any other elegant alternative in Scala? Can I keep using an ActiveRecord style or is it a total anti-pattern for testability?

Thanks


Solution

  • I'd definitely go with the Cake Pattern.

    You can read the following article with show an example of how to use the Cake Pattern in a Play2 application: http://julien.richard-foy.fr/blog/2011/11/26/dependency-injection-in-scala-with-play-2-it-s-free/