Akka Play测试与@NamedCache注射



我在Akka Play测试中遇到了问题。我有这个班:

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)

最新更新