2012-05-17 14 views
7

के साथ यूनिट परीक्षण आरईएसटी कंट्रोलर मैंने अपनी स्प्रिंग निर्भरता को वसंत 3.1.1.RELEASE में अपग्रेड कर दिया है और मैं एक सरल नियंत्रक इकाई परीक्षण के लिए spring-test-mvc का उपयोग करने की कोशिश कर रहा हूं। मैं Spring REST Controller Test with spring-test-mvc framework में उपयोग की जाने वाली तकनीक का पालन कर रहा हूं, क्योंकि ऐसा लगता है कि उस व्यक्ति के लिए काम किया है, लेकिन मैं अब तक असफल रहा हूं। मुझे लगता है कि कुछ प्रमुख विन्यास मैं "अपने परीक्षण संदर्भ फ़ाइल में लापता मीटर है।स्प्रिंग-टेस्ट-एमवीसी

मैं कोई त्रुटि मिलती है। क्योंकि 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

वसंत-परीक्षण-एमवीसी वास्तव में आशाजनक है, लेकिन दस्तावेज़ीकरण की कमी है। क्या आप इस बिंदु पर रीडमे के अलावा कुछ भी जानते हैं? –

+0

@ माइकपार्ट्रिज मुझे इसके बारे में मिली सारी जानकारी उनके गिथब साइट से है। –

उत्तर

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 अपवाद उत्पन्न किया जा रहा था, लेकिन अपवाद के विवरण या इसके कारण क्या नहीं था।

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

जो मैं जोड़कर तय:

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

यह निम्न अपवाद का पता चला: विवरण देखने के लिए, मैं प्रतिक्रिया शरीर के लिए अपवाद विवरण लिखने के लिए नियंत्रक वर्ग में जोड़ने के लिए किया था अपने मॉडल कक्षा में setters को @JsonProperty एनोटेशन:

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

आपने "प्रिंट()" विधि का क्या उल्लेख किया है? इसे टेस्ट क्लास में नहीं मिला है और चूंकि आपकी टेस्ट क्लास कहीं से भी नहीं बढ़ती है, क्या आप यह बता सकते हैं कि आपको यह कहां से मिला या यह क्या करता है। धन्यवाद। –

+2

@MathiasLin मैं इसे स्थिर रूप से आयात करके प्रिंट() विधि का उपयोग करता हूं, जैसे: 'स्थिर static org.springframework.test.web.server.result.MockMvcResultHandlers.print;' यह विधि भेजी गई अनुरोध के बारे में विवरण प्रिंट करती है। यह डिबगिंग में बहुत उपयोगी है। –

+3

को इंगित करना चाहिए कि वास्तविक आयात विवरण निम्नानुसार है: 'स्थिर static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print' आयात करें – leojh

0

वसंत परीक्षण-MVC विरूपण साक्ष्य का एक good presentation, निम्नलिखित प्रस्तुति के अंत में है मैं टी दस्तावेज़ के पृष्ठ 116 के आसपास शुरू होता है।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^