2012-05-17 310 views
7

我已經升級了我的Spring依賴到Spring 3.1.1.RELEASE,我試圖用spring-test-mvc來單元測試一個簡單的控制器。我一直在遵循Spring REST Controller Test with spring-test-mvc framework中使用的技術,因爲它似乎已經爲那個人工作,但迄今爲止我一直沒有成功。我覺得有一些關鍵配置I「M在我的測試方面的文件丟失。單元測試REST控制器與彈簧測試mvc

我沒有得到任何錯誤。我知道它不工作的原因是因爲Hello World永遠不會被打印(見控制器)。我缺少什麼嗎?

控制器:

@Controller 
@RequestMapping("/debug") 
public class DebugOutputController { 

    @RequestMapping(method = RequestMethod.POST) 
    public void saveDebugOutput(@RequestBody DebugOutput debugOutput, HttpServletResponse response) { 
     System.out.println("Hello World"); 
    } 
} 

測試類:

@RunWith(SpringJUnit4ClassRunner.class) //this lets tests access Spring beans defined in the context config file 
@ContextConfiguration(locations={"file:src/test/resources/itest/restAPITestContext.xml"}) //tells the test where to get configuration and beans to be used by the test. 
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) //overrides the default stack of listeners 
public class ITRestAPI{ 

@Autowired 
private DebugOutputController debugOutputController; 

private MockMvc mockMvc; 

@Before 
public void setUp() throws Exception { 
    mockMvc = MockMvcBuilders.standaloneSetup(debugOutputController).build(); 
} 

@After 
public void tearDown() throws Exception { 
} 

@Test 
public void shouldPerformPost() throws Exception { 
    this.mockMvc.perform(post("/debug")); 
} 
} 

restAPITestContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <mvc:annotation-driven /> 
    <mvc:default-servlet-handler /> 
    <context:component-scan resource-pattern="*DebugOutputController*" base-package="com.company.project.servlet" />  

</beans> 
+0

spring-test-mvc確實很有前途,但缺少文檔。在這一點上你知道除README之外的任何內容嗎? –

+0

@MikePartridge我發現的關於它的所有信息都來自他們的Github網站。 –

回答

14

原來的HttpMessageNotReadable異常正在發生,我只是看不到它,因爲我沒有登錄或任何打印。我發現它使用DefaultRequestBuilder類建築的HTTP請求在我的測試類,並增加了andDo(print())

DefaultRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/debug").contentType(MediaType.APPLICATION_JSON).body(new String("{\"T1\":109.1, \"T2\":99.3}").getBytes()); 
this.mockMvc.perform(requestBuilder).andDo(print()); 

所以在此之後,使用的andDo(print())輸出,我可以看到HttpMessageNotReadable異常被拋出,但不知道例外的細節或是什麼造成的。要查看細節,我不得不把它添加到控制器類寫異常的詳細信息,以響應正文:

@ExceptionHandler(HttpMessageNotReadableException.class) 
@ResponseBody 
public String handleException1(HttpMessageNotReadableException ex) 
{ 
    return ex.getMessage(); 
} 

這揭示了以下異常:

Could not read JSON: Unrecognized field "T1" (Class com.company.project.model.device.DebugOutput), not marked as ignorable 

我固定通過增加@JsonProperty註釋在我的模型類的制定者:

@JsonProperty("T1") 
public void setT1(Float t1) { 
    T1 = t1; 
} 
+0

你提到的這個「print()」方法是什麼?無法在測試類中找到它,並且由於您的測試類不能從任何地方擴展,您能指出從哪裏獲得它或者它做了什麼。謝謝。 –

+2

@MathiasLin我通過靜態導入它來使用print()方法,如下所示:'import static org.springframework.test.web.server.result.MockMvcResultHandlers.print;'此方法打印有關發送請求的詳細信息。它在調試中非常有用。 –

+3

應該指出實際的導入語句如下:'import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print' – leojh