我试图在注入(inject)器中创建以测试 api。

The test binding are as follows
  class ApiTestModule extends AbstractModule {
    def configure(): Unit = {
      bind(classOf[Client]).in(classOf[Singleton])
      bind(classOf[ApiGetController]).in(classOf[Singleton])
      bind(classOf[ApiPostController]).in(classOf[Singleton])
      bind(classOf[IndexService]).in(classOf[Singleton])
      bind(classOf[PanelService]).in(classOf[Singleton])
      bind(classOf[EntitiesService]).in(classOf[Singleton])
      bind(classOf[AuthenticatedPublicApiAction]).in(classOf[Singleton])
      bind(classOf[RateLimitedApiAction]).in(classOf[Singleton])
      bind(classOf[ApiKeyValidatorUpdaterThread]).in(classOf[Singleton])
      bind(classOf[ApiKeyValidatorService]).in(classOf[Singleton])
      bind(classOf[ArticleFrontService]).in(classOf[Singleton])

    }

我在代码中创建了 te 注入(inject)器
 val testModule = new AbstractModule() {
    def configure() = {
      new ApiTestModule().configure(binder())
    }
  }
  val injector = new GuiceApplicationBuilder()
    .overrides(testModule).injector()

  val apiGetController = injector.instanceOf(classOf[ApiGetController])

我收到以下错误

异常或错误导致运行中止:无法创建注入(inject)器,请参阅以下错误:
1) No implementation for org.elasticsearch.client.Client annotated with @com.google.inject.name.Named(value=SpikeClient) was bound.
  while locating org.elasticsearch.client.Client annotated with @com.google.inject.name.Named(value=SpikeClient)
    for parameter 2 at com.newswhip.api.service.ApiArticleService.<init>(ApiArticleService.scala:19)
  while locating com.newswhip.api.service.ApiArticleService
    for parameter 1 at com.newswhip.api.controllers.ApiGetController.<init>(ApiGetController.scala:57)
  at com.newswhip.spike.inject.TestBindings$ApiTestModule.configure(TestBindings.scala:133) (via modules: com.google.inject.util.Modules$OverrideModule -> com.newswhip.api.service.ApiArticleServiceTest$$anon$1)

2) No implementation for org.elasticsearch.client.Client annotated with @com.google.inject.name.Named(value=SpikeClient) was bound.
  while locating org.elasticsearch.client.Client annotated with @com.google.inject.name.Named(value=SpikeClient)
    for parameter 2 at com.newswhip.api.service.ApiArticleService.<init>(ApiArticleService.scala:19)
  while locating com.newswhip.api.service.ApiArticleService
    for parameter 1 at com.newswhip.api.controllers.ApiPostController.<init>(ApiPostController.scala:17)
  at com.newswhip.spike.inject.TestBindings$ApiTestModule.configure(TestBindings.scala:134) (via modules: com.google.inject.util.Modules$OverrideModule -> com.newswhip.api.service.ApiArticleServiceTest$$anon$1)

3) No implementation for org.elasticsearch.client.Client annotated with @com.google.inject.name.Named(value=SpikeClient) was bound.
  while locating org.elasticsearch.client.Client annotated with @com.google.inject.name.Named(value=SpikeClient)
    for parameter 0 at com.newswhip.spike.article.service.SpikeEsQueryBuilder.<init>(SpikeEsQueryBuilder.scala:20)
  while locating com.newswhip.spike.article.service.SpikeEsQueryBuilder
    for parameter 2 at com.newswhip.spike.article.service.EntitiesService.<init>(EntitiesService.scala:30)
  at com.newswhip.spike.inject.TestBindings$ApiTestModule.configure(TestBindings.scala:137) (via modules: com.google.inject.util.Modules$OverrideModule -> com.newswhip.api.service.ApiArticleServiceTest$$anon$1)

4) No implementation for org.elasticsearch.client.Client was bound.
  at com.newswhip.spike.inject.TestBindings$ApiTestModule.configure(TestBindings.scala:131) (via modules: com.google.inject.util.Modules$OverrideModule -> com.newswhip.api.service.ApiArticleServiceTest$$anon$1)

5) No implementation for play.api.db.Database was bound.
  at com.newswhip.spike.inject.TestBindings$ApiTestModule.configure(TestBindings.scala:132) (via modules: com.google.inject.util.Modules$OverrideModule -> com.newswhip.api.service.ApiArticleServiceTest$$anon$1

)

我认为问题出在我的绑定(bind)模块中,但我无法弄清楚问题是什么。任何帮助将不胜感激。

最佳答案

Mockito 可用于测试。我想你可以像下面的注入(inject)一样尝试用 guice 进行测试

class TestModule extends AbstractModule with MockitoSugar {

  val mockClient: Client = mock[Client]

  override def configure(): Unit = {
    bind(classOf[Client]).toInstance(mockClient)
  }

}

很容易模拟和 stub 。如果你想在测试中模拟或 stub ,你可以像这样使用
doReturn("dummyResult").when(testModule.mockClient).someMethod()

关于scala - Guice ,无法创建注入(inject)器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43275988/

10-16 02:35