के साथ यूनिट परीक्षण आरईएसटी कंट्रोलर मैंने अपनी स्प्रिंग निर्भरता को वसंत 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>
वसंत-परीक्षण-एमवीसी वास्तव में आशाजनक है, लेकिन दस्तावेज़ीकरण की कमी है। क्या आप इस बिंदु पर रीडमे के अलावा कुछ भी जानते हैं? –
@ माइकपार्ट्रिज मुझे इसके बारे में मिली सारी जानकारी उनके गिथब साइट से है। –