我试图在进样器中创建用于测试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注入器
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
)
我认为问题出在我的绑定模块中,但我无法弄清问题是什么。任何帮助将不胜感激。
答案 0 :(得分: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()
答案 1 :(得分: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
}
}
}