【问题标题】:null URL in testing with spring使用弹簧进行测试时的 URL 为空
【发布时间】:2013-08-23 15:04:27
【问题描述】:

我正在我的 spring 控制器中测试一个方法,当它到达 sendRequest 方法时,我得到了一个用于 url 的空指针。我的测试方法如下:

@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);
}

我的助手类:

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");
}
}

请求引用变量属于 MockHttpServletRequest 类,在我的控制器中它标有@RequestMapping(method = RequestMethod.GET, value = "/userInfo"),任何帮助将不胜感激。

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>`

我的所有测试都在一个测试目录中,在我的 controller.xml 中我有:

<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>`

【问题讨论】:

  • "http://localhost:8080/userInfo.htm" 也许?

标签: java spring-mvc junit


【解决方案1】:

如果一旦执行流到达sendRequest 方法,它就会抛出NullPointerExceptionrequesthandlerAdapter 对象为空,因为您正在访问的对象只有这些对象,并且可能会抛出@987654325 @。

如果要检查,请尝试记录 2 对象状态:

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();
}

您能否发布完整的课程以便您提供更详细的帮助?

【讨论】:

  • 看来我的 handlerAdapter 为空,我一定是接线不正确。谢谢!
  • 您要我发布我的 applicationContext 吗?
  • 我在我的测试目录中发布了我的applicationContext.xml,它调用了controller.xml,它为我的控制器连接了bean,并连接了我的HandlerAdapter
  • @invictvs 你的问题还是一样还是变了?
【解决方案2】:

您似乎是通过调用sendRequest("/userInfo.htm", GET, userController, params); 直接调用控制器方法。这意味着调用不会通过 Spring 上下文执行,因此 Spring 没有做任何事情来填充 request 字段。

考虑看看这个帖子:how-to-unit-test-a-spring-mvc-controller-using-pathvariable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2015-12-29
    • 2011-11-27
    • 2019-02-03
    • 2022-07-21
    相关资源
    最近更新 更多