【问题标题】:Akka Play testing with @NamedCache injections使用 @NamedCache 注入进行 Akka Play 测试
【发布时间】:2021-12-26 11:51:21
【问题描述】:

我在使用 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")

我如何命名我的测试缓存以便注入传递?

【问题讨论】:

    标签: scala playframework


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 2014-01-19
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多