Search code examples
scalaplayframework

Akka Play testing with @NamedCache injections


I am having troubles with Akka Play testing. I have this class:

class Setvice @Inject() (
    @NamedCache("token-cache") tokenCache: AsyncCacheApi
)

And have this injector in test:

private val cache: MockCaffeineCache = new CaffeineCacheApi(new NamedCaffeineCache("token-cache", new MockCaffeineCache)) // named cache not helping here


val application: Injector = new GuiceInjectorBuilder()
    {some bindings here}
    .bindings(bind[AsyncCacheApi].qualifiedWith("token-cache").to(cache))

As far as i know qualifiedWith is a different thing so it won't help here either.

Error:

[info] - should return search result *** FAILED ***
[info]   com.google.inject.ConfigurationException: Guice configuration errors:
[info] 
[info] 1) No implementation for play.api.cache.AsyncCacheApi annotated with @play.cache.NamedCache(value="token-cache") was bound.
[info]   Did you mean?
[info]     * play.api.cache.AsyncCacheApi annotated with @com.google.inject.name.Named(value="token-cache")

How do i name my test cache so it will be passed in injection?


Solution

  • I found the solution. When you just use the qualifiedWith(string) you are specifying the name of @Named annotation. But there is an overloaded method with abstract annotation type:

    bind[AsyncCacheApi].qualifiedWith[NamedCache](new NamedCacheImpl("token-cache")).to(cache)