scala - Guice , unable to create injector -
im trying create in injector testing api.
the test binding 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]) }
i've created te injector in code
val testmodule = new abstractmodule() { def configure() = { new apitestmodule().configure(binder()) } } val injector = new guiceapplicationbuilder() .overrides(testmodule).injector() val apigetcontroller = injector.instanceof(classof[apigetcontroller])
and i'm getting following error
exception or error caused run abort: unable create injector, see following errors:
1) no implementation org.elasticsearch.client.client annotated @com.google.inject.name.named(value=spikeclient) bound. while locating org.elasticsearch.client.client annotated @com.google.inject.name.named(value=spikeclient) parameter 2 @ com.newswhip.api.service.apiarticleservice.<init>(apiarticleservice.scala:19) while locating com.newswhip.api.service.apiarticleservice parameter 1 @ com.newswhip.api.controllers.apigetcontroller.<init>(apigetcontroller.scala:57) @ 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 org.elasticsearch.client.client annotated @com.google.inject.name.named(value=spikeclient) bound. while locating org.elasticsearch.client.client annotated @com.google.inject.name.named(value=spikeclient) parameter 2 @ com.newswhip.api.service.apiarticleservice.<init>(apiarticleservice.scala:19) while locating com.newswhip.api.service.apiarticleservice parameter 1 @ com.newswhip.api.controllers.apipostcontroller.<init>(apipostcontroller.scala:17) @ 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 org.elasticsearch.client.client annotated @com.google.inject.name.named(value=spikeclient) bound. while locating org.elasticsearch.client.client annotated @com.google.inject.name.named(value=spikeclient) parameter 0 @ com.newswhip.spike.article.service.spikeesquerybuilder.<init>(spikeesquerybuilder.scala:20) while locating com.newswhip.spike.article.service.spikeesquerybuilder parameter 2 @ com.newswhip.spike.article.service.entitiesservice.<init>(entitiesservice.scala:30) @ 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 org.elasticsearch.client.client bound. @ 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 play.api.db.database bound. @ 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
)
i think issue in my bindings module cant figure out issue is. appreciated.
mockito useful testing. think can try below injection testing guice
class testmodule extends abstractmodule mockitosugar { val mockclient: client = mock[client] override def configure(): unit = { bind(classof[client]).toinstance(mockclient) } }
it easy mocking , stubbing. if want mock or stub in test can use this
doreturn("dummyresult").when(testmodule.mockclient).somemethod()
Comments
Post a Comment