Unable to invoke method in mocked service from mocked controller using mockito with spring web mvc -
i trying create junit test case spring mvc rest controller , service accessed controller.
using mockito doing above. i'm able invoke mock injected controller test case , when debug see mocked userservice available in mocked contoller, method within service object not getting invoked. while debugging observe steps on service method call.
i not see kind of exception too.
i using
- maven 3
- spring 4.1.1 release version
- junit 4.11
- java version 1.6
i have pasted code below:
1. test class
package controller; import java.io.ioexception; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.mockito.injectmocks; import org.mockito.mock; import org.mockito.mockitoannotations; import org.springframework.http.mediatype; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.requestbuilder; import org.springframework.test.web.servlet.request.mockmvcrequestbuilders; import org.springframework.test.web.servlet.result.mockmvcresultmatchers; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import com.fasterxml.jackson.databind.objectmapper; import com.mypackage.model.user; import com.mypackage.service.iuserservice; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath:/spring/application-config.xml") public class usercontrollertest { private mockmvc mockmvc; @mock private iuserservice userservice; @injectmocks private usercontroller controller; @before public void setup(){ mockitoannotations.initmocks(this); this.mockmvc = mockmvcbuilders.standalonesetup(controller).build(); } @test public void testregisteruser()throws exception{ user user = new user(); user.setcity("city"); user.settown("town"); user.setfirstname("name"); user.setlastname("lastname"); user.setpassword("abc@123"); user.setusername("abc@gmail.com"); requestbuilder builder = mockmvcrequestbuilders.post("/service/user/register/") .contenttype(mediatype.application_json).content(convertobjecttojsonbytes(user)); mockmvc.perform(builder).andexpect(mockmvcresultmatchers.status().isok()); } public static byte[] convertobjecttojsonbytes(object object) throws ioexception { objectmapper mapper = new objectmapper(); return mapper.writevalueasbytes(object); } }
2. controller under test
package com.mypackage.controller; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import com.mypackage.model.user; import com.mypackage.service.iuserservice; @restcontroller @requestmapping("/service/user") public class usercontroller { private static final logger logger = loggerfactory.getlogger(usercontroller.class); @autowired private iuserservice userservice; public iuserservice getuserservice() { return userservice; } public void setuserservice(iuserservice userservice) { this.userservice = userservice; } @requestmapping(value = "/register/", method = requestmethod.post,headers="accept=application/json") public string registeruser(@requestbody user user) { logger.info("register user:"+user.tostring()); string userdetails = userservice.registeruser(user); logger.info("user registered:"+userdetails); return userdetails; } }
3. service invoked within controller in test
the service method registeruser invoked controller above. not see logs getting printed on console when run test case. when debugging see userserivice instance of type - iuserservice$$enhancerbymockitowithcglib$$c00081eb created when dig deep see list of methods under mockhandlers registered methods, see 'tostring' method in list of invocations. not sure if gives indication on why method 'registeruser' in service class below not getting invoked during test case.
package com.mypackage.service; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.security.core.authentication; import org.springframework.security.core.grantedauthority; import org.springframework.stereotype.service; import com.mypackage.dao.iuserdao; import com.mypackage.model.user; import com.mypackage.model.userinfo; @service("userservice") public class userservice implements iuserservice { private static final logger logger = loggerfactory.getlogger(userservice.class); @autowired private iuserdao userdao; public iuserdao getuserdao() { return userdao; } public void setuserdao(iuserdao userdao) { this.userdao = userdao; } @override public string registeruser(user user) { // todo auto-generated method stub logger.info("new user registration:",user); if(user!=null && user.getusername()!=null){ user alreadyexist = userdao.getuserbylogin(user.getusername()); if(alreadyexist!=null){ return "useralreadyexists"; } } logger.info("new user registration complete:",user.getuser_id()); return null; } }
4. interface implemented userservice class in point 3 above
iuserservice. mocked userservice in test class above of type iuserservice.
package com.mypackage.service; import com.mypackage.model.user; import com.mypackage.model.userinfo; public interface iuserservice { public string registeruser(user user); }
your method (read code) within userservice never invoked controller because, has been mocked out mockito.
you have defined doing:
@mock private iuserservice userservice; @injectmocks private usercontroller controller; mockitoannotations.initmocks(this);
if want assert method has been called can using,
verify(userservice, times(1)).registeruser(any(user.class));
Comments
Post a Comment