Sau*_*abh 7 junit spring spring-mvc junit4 junit3
嗨,我是Spring和Junit的新手.我的控制器中有一个方法.我想为这个方法编写Junit(getPersons()).
@Autowired
private PersonService personService;
@RequestMapping(value="/t2/{yy_id}/person", method=RequestMethod.GET)
@ResponseBody
public PersonInfo[] getPersons() {
return personService.getPersons();
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我并以正确的方式指导我.请举个例子.
您应该使用mvc测试框架.除了您自己的协作者之外,它还允许您测试控制器周围的所有mvc基础设施 - 例如@RequestMapping,@ResponseBody等等.
使用该框架的一个非常简单的示例是调用该getPersons()方法并声明200收到响应代码:
...
@Test
public void getPersons() throws Exception {
this.mockMvc.perform(get("/t2/1234/person"))
.andExpect(status().isOk());
}
...
Run Code Online (Sandbox Code Playgroud)
该框架能够做得更多,但我恳请您阅读文档,其中包含大量示例.我希望有所帮助.