Single REST controller testing with spring -test-mvc

I updated Spring's dependency on Spring 3.1.1.RELEASE, and I'm trying to use spring-test-mvc to unit test a simple controller. I followed the technique used in the Spring Test of the REST controller with the spring -test-mvc framework , as it seems to have worked for this person, but I still have not been successful. I think there is some kind of key configuration that I do not see in the test context file.

I get no errors. The reason I know that it does not work is because Hello World never prints (see Controller). What am I missing here?

Controller:

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

Testing Class:

 @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> 
+7
source share
2 answers

An HttpMessageNotReadable exception is HttpMessageNotReadable , and I just couldn't see it, because I didn't write or print it anywhere. I found it by building an HTTP request in my test class using the DefaultRequestBuilder class and adding 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()); 

So after that, using the output of andDo(print()) , I could see that the HttpMessageNotReadable exception was thrown, but did not know the details of the exception or what caused it. To see the details, I had to add this to the controller class in order to write the exception details in the response body:

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

This revealed the following exception:

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

which I fixed by adding @JsonProperty annotation to the setters in my model class:

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

A good presentation of the spring -test-mvc artifact is at the end of the next presentation, it starts on page 116 of the document.

0
source

All Articles