Cheefachi
Cheefachi

Reputation: 125

Play 2.2: Problems unit testing code using Play Caching (Scala)

I have thoroughly searched the interwebs and cannot find a solution. Kind of surprising considering this should be a simple enough task to do.

We are using Redis in our Play application and as part of that are disabling the default ehcache implementation. However in our unit tests, when running, it cannot connect to Redis (the error is "ex:redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool"). So what I'd like to do is not bring in Redis in the test and use the default ehcache implementation (I believe that the application.conf that disables the ehcache does not apply to the unit test). So the top of my unit test looks like this:

class MyTest extends FunSuite with MockitoSugar with BeforeAndAfter {
  val app = FakeApplication(
    additionalPlugins = Seq("play.api.cache.EhCachePlugin"),
    withoutPlugins = Seq("com.typesafe.plugin.RedisPlugin")
  )

and am running each test with:

running(app) {...

When I run play test I am getting: "There is no cache plugin registered. Make sure at least one CachePlugin implementation is enabled.". Does this mean that I am not getting the name of the default cache plugin correct? I tried using "play.api.cache.CachePlugin" but that cannot be found. What should be the name of the plugin?

Is this the right approach? I have seen examples using inMemoryDatabase but I can't get it to even compile for me. Play cannot find inMemoryDatabase() by itself and while it can find Helpers.inMemoryDatabase(), the compiler complains that it returns a Map[String,String] but additionalConfiguration requires Map[String,_]. I am using play 2.2.0-M2.

Upvotes: 2

Views: 1382

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

It seems like it's not enough to add/remove plugins via your method, as it's still using the ehcacheplugin=disabled from your config. You don't actually need additionalPlugins = Seq("play.api.cache.EhCachePlugin") either, as Play loads this plugin in the default configuration, even if it's disabled (well, sort of.. it's certainly not doing what I expected).

Using additionalConfiguration is the way to go:

val app = FakeApplication(
    additionalConfiguration = Map(
        "ehcacheplugin" -> "enabled"
    ),
    withoutPlugins = Seq("com.typesafe.plugin.RedisPlugin")
 )

As a side note, it may also be worthwhile for you to upgrade to at least Play 2.2.3, as 2.2-M2 is quite old at this point and not an official release.

Upvotes: 1

Related Questions