喜欢使用@NamedCache 注入进行测试

Akka Play testing with @NamedCache injections

我在进行 Akka Play 测试时遇到了问题。我有这个 class:

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

并测试此注入器:

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))

据我所知,qualifiedWith 是另一回事,所以在这里也无济于事。

错误:

[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")

如何命名我的测试缓存以便它在注入中通过?

我找到了解决方案。当您只使用 qualifiedWith(string) 时,您指定的是 @Named 注释的名称。但是有一个抽象注解类型的重载方法:

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