java - null URL in testing with spring -
i testing method in spring controller , getting null pointer url when hits sendrequest method. test method follows:
@test public void testshowform() { map<string, object> params = new hashmap<string, object>(); params.put("email", "testemail"); params.put("accesscode", keygenerator.getkey64()); string result = sendrequest("/userinfo.htm", get, usercontroller, params); assertnotnull(result); }
my helper class:
public class junitcontrollerhelper extends junithelper { @autowired protected junitdatahelper junitdatahelper; @autowired protected junitservicehelper junitservicehelper; @autowired protected applicationcontext applicationcontext; protected final string = "get"; protected final string post = "post"; protected mockhttpservletrequest request; protected mockhttpservletresponse response; protected handleradapter handleradapter; @before public void setup() { request = new mockhttpservletrequest(); response = new mockhttpservletresponse(); handleradapter = applicationcontext.getbean(handleradapter.class); } public string sendrequest(string url, string method, object controller, map<string, object> params) throws exception { request.setrequesturi(url); request.setparameters(params); request.setmethod(method); request.setcontenttype("application/json"); handleradapter.handle(request, response, controller); return response.getcontentasstring(); } public static void assertsubmitsuccess(string json) { if(!json.contains("\"success\":true")) fail("submit returned unexpected errors"); } public static void assertsubmiterror(string field, string json) { if(!json.contains("\"success\":false") || !json.contains("\"errors\"") || !json.contains("\""+field+"\"")) fail("submit did not return expected errors"); } }
the request reference variable of mockhttpservletrequest class , in controller labeled @requestmapping(method = requestmethod.get, value = "/userinfo")
, appreciated.
applicationcontext.xml:
<import resource="classpath:springconfig/dao.xml"/> <import resource="classpath:springconfig/database.xml"/> <import resource="classpath:springconfig/service.xml"/> <import resource="classpath:springconfig/validator.xml"/> <import resource="data.xml"/> <import resource="controller.xml"/> <import resource="junit.xml"/> </beans>`
i have tests in test directory, in controller.xml have:
<bean id="userinfocontroller" class="com.ck.web.controller.usercontroller"/> <bean class="org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping"/> <bean id="jacksonmessageconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter"/></bean> <bean class="org.springframework.web.servlet.mvc.annotationmethodhandleradapter"/></bean>`
if once execution flow reaches sendrequest
method, throws nullpointerexception
, request
or handleradapter
object null because objects accessing , throw nullpointerexception
.
if want check, try log 2 object state:
public string sendrequest(string url, string method, object controller, map<string, object> params) throws exception { // log object state here or give print in console (as prefer) request.setrequesturi(url); request.setparameters(params); request.setmethod(method); request.setcontenttype("application/json"); handleradapter.handle(request, response, controller); return response.getcontentasstring(); }
can post complete class in order yo supply more detailed help?
Comments
Post a Comment