1

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<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 = "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 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.

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 all my tests in a test directory, in my controller.xml I 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>`
invictvs1
  • 409
  • 1
  • 5
  • 18

2 Answers2

0

If once the execution flow reaches the sendRequest method, it throws a NullPointerException, the request or the handlerAdapter object is null because are the only objects that you are accessing and that could throw a NullPointerException.

If you want to check, try to log the 2 object state:

public String sendRequest(String url, String method, Object controller, Map<String, Object> params) throws Exception {

    // log the object state here or give it a print in console (as you prefer)

    request.setRequestURI(url);
    request.setParameters(params);
    request.setMethod(method);
    request.setContentType("application/json");
    handlerAdapter.handle(request, response, controller);
    return response.getContentAsString();
}

Can you post the complete class in order yo supply a more detailed help?

araknoid
  • 2,805
  • 4
  • 30
  • 32
0

It appears that you are invoking the controller method directly via the call sendRequest("/userInfo.htm", GET, userController, params);. This means that the call does NOT execute through a Spring context and therefore Spring is not doing anything to populate the request field.

Consider looking at this post: how-to-unit-test-a-spring-mvc-controller-using-pathvariable

Community
  • 1
  • 1
John B
  • 30,460
  • 6
  • 67
  • 92