2013-08-23 85 views
1

我正在測試我的彈簧控制器中的一個方法,並且在它遇到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>` 
+0

'的 「http://本地主機:8080/userInfo.htm」'可能? –

回答

0

如果一旦執行流到達sendRequest方法,它會拋出一個NullPointerException,requesthandlerAdapter對象爲空,因爲是您正在訪問的唯一對象,並且可能會拋出一個NullPointerException

如果你想檢查一下,嘗試登錄的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(); 
} 

你能後的完整的類,以便喲提供更詳細的幫助?

+0

它似乎我的handlerAdapter爲空,我必須錯誤地將它連線。謝謝! – invictvs1

+0

你想我發佈我的applicationContext嗎? – invictvs1

+0

@invictvs如果你的問題仍然沒有解決,爲什麼不。 – araknoid

0

看起來您正在通過調用sendRequest("/userInfo.htm", GET, userController, params);直接調用控制器方法。這意味着調用不會通過Spring上下文執行,因此Spring不會執行任何操作來填充request字段。

考慮找這個帖子:how-to-unit-test-a-spring-mvc-controller-using-pathvariable