我正在測試我的彈簧控制器中的一個方法,並且在它遇到sendRequest方法時爲它獲取一個空指針。我的測試方法如下:null在彈簧測試中的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://本地主機:8080/userInfo.htm」'可能? –