CFL*_*eff 9 spring unit-testing spring-mvc spring-test
我已将Spring依赖项升级到Spring 3.1.1.RELEASE,并且我正在尝试使用spring-test-mvc对一个简单的Controller进行单元测试.我一直在使用spring-test-mvc框架跟踪Spring REST Controller Test中使用的技术,因为它似乎对那个人起作用,但到目前为止我还没有成功.我认为我的测试上下文文件中缺少一些关键配置.
我没有错.我知道它不起作用的原因是因为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");
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
@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"));
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
CFL*_*eff 17
事实证明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());
Run Code Online (Sandbox Code Playgroud)
所以在那之后,使用输出andDo(print()),我可以看到HttpMessageNotReadable异常被抛出,但不知道异常的细节或导致它的原因.要查看详细信息,我必须将其添加到控制器类,以将异常详细信息写入响应主体:
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public String handleException1(HttpMessageNotReadableException ex)
{
return ex.getMessage();
}
Run Code Online (Sandbox Code Playgroud)
这揭示了以下例外情况:
Could not read JSON: Unrecognized field "T1" (Class com.company.project.model.device.DebugOutput), not marked as ignorable
Run Code Online (Sandbox Code Playgroud)
我通过将@JsonProperty注释添加到我的模型类中的setter来修复:
@JsonProperty("T1")
public void setT1(Float t1) {
T1 = t1;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21286 次 |
| 最近记录: |