2017-04-07 91 views
2

我試圖在注入器中創建測試api。Guice,無法創建注入器

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注入代碼

val testModule = new AbstractModule() { 
    def configure() = { 
     new ApiTestModule().configure(binder()) 
    } 
    } 
    val injector = new GuiceApplicationBuilder() 
    .overrides(testModule).injector() 

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

而且我發現了以下錯誤

異常或錯誤導致的運行中止:無法創建噴油器,請參閱以下錯誤:

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 

我認爲問題出在我的綁定模塊中,但我無法弄清楚問題所在。任何幫助將不勝感激。

回答

2

Mockito對測試非常有用。我認爲你可以嘗試像下面的注射用於測試與guice

class TestModule extends AbstractModule with MockitoSugar { 

    val mockClient: Client = mock[Client] 

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

} 

這是很容易嘲笑和殘樁。如果你想測試模擬或存根,你可以使用這樣

doReturn("dummyResult").when(testModule.mockClient).someMethod() 
0

看一看Guice's BoundFields。然後你可以創建一個像這樣的測試:

public class TestFoo extends WordSpec with BeforeAndAfterEach with MockitoSugar { 
    // bind(Bar.class).toInstance(barMock); 
    @Bind var barMock: Bar = _ 

    // Foo depends on Bar. 
    @Inject var foo: Foo = _ 

    override def beforeEach() = { 
    barMock = mock[Bar]; 
    Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this); 
    } 

    "your Test" should { 
    "do" in { 
     // your test case 
    } 
    } 
}