Friday, 23 August 2013
null URL in testing with spring
null URL in testing with spring References I am testing a method in my spring controller and am getting a null pointer for the url when it hits the sendRequest method. My test method is as follows: @Test public void testShowForm() { Map params = new HashMap(); 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 = \"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 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 is of the MockHttpServletRequest class and in my controller it is labeled with @RequestMapping(method = RequestMethod.GET, value = \"/userInfo\"), any help would be greatly appreciated.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment