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