scala - Mocked service class is not working as expected while testing controller with Guice -
i have controller uses dependency injection , looks that:
class formfieldvalues @inject() (ffvaluesrepo : formfieldvaluerepository) extends controller classlogger { /** * gets formfieldvalue obejct checllist revision id * * @param revid * @return */ def getall(revid: muid) = action { implicit request => db.withtransaction { implicit connection => logger.info("injected repo: " + ffvaluesrepo) val values = ffvaluesrepo.getallforchecklistrevision(revid) logger.info("values: " + values) // forse json ok(json.tojson(values)).as("application/json") } } }
the actual execution of controller works expected. however, when trying build test with mocked repository, mocking not seem picking properly.
this current test:
class formfieldvaluesspec extends specification mockito{ lazy val mockffvaluerepo = mock[formfieldvaluerepository] implicit val connection = mock[connection] val mockedservicesapp = new guiceapplicationbuilder() .overrides(bind[formfieldvaluerepository].toinstance(mockffvaluerepo)) .build() "formfieldvalues#getall" should { "return json object relevant list of field values checklist revision id" in new withapplication(mockedservicesapp) { // lets try empty list first val emptylist = list() doreturn(emptylist).when(mockffvaluerepo).getallforchecklistrevision(muid.fromstring("r5r3m4mwqzvakjcsikfjfg").get) val some(result) = route(fakerequest(get, "/1/checklist-revisions/r5r3m4mwqzvakjcsikfjfg/form-field-values")) status(result) must equalto(ok) } } }
in logs see this:
2016-01-15 20:34:50,450 info c.w.formfieldvalues - injected repo: mock formfieldvaluerepository, hashcode: 1337346192 2016-01-15 20:34:50,463 info c.w.formfieldvalues - values: null
which means object injected correctly, reason instead of returning per stub (empty list), return null.
update after digging around got work replacing mock spy:
val mockffvaluerepo = spy(new anormformfieldvaluerepository()) implicit val connection = mock[connection]
so looks mock didn't work... how can work proper mock?
what doing wrong?
thanks,
Comments
Post a Comment