Spring MVC测试框架详解——客户端测试
编程技术  /  houtizong 发布于 3年前   62
上一篇《Spring MVC测试框架详解——服务端测试》已经介绍了服务端测试,接下来再看看如果测试Rest客户端,对于客户端测试以前经常使用的方法是启动一个内嵌的jetty/tomcat容器,然后发送真实的请求到相应的控制器;这种方式的缺点就是速度慢;自Spring 3.2开始提供了对RestTemplate的模拟服务器测试方式,也就是说使用RestTemplate测试时无须启动服务器,而是模拟一个服务器进行测试,这样的话速度是非常快的。
整个环境在上一篇《Spring MVC测试框架详解——服务端测试》基础上进行构建。
@RestController@RequestMapping("/users")public class UserRestController { private UserService userService; @Autowired public UserRestController(UserService userService) { this.userService = userService; } @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public User findById(@PathVariable("id") Long id) { return userService.findById(1L); } @RequestMapping(method = RequestMethod.POST) public ResponseEntity<User> save(@RequestBody User user, UriComponentsBuilder uriComponentsBuilder) { //save user user.setId(1L); MultiValueMap headers = new HttpHeaders(); headers.set("Location", uriComponentsBuilder.path("/users/{id}").buildAndExpand(user.getId()).toUriString()); return new ResponseEntity(user, headers, HttpStatus.CREATED); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@RequestBody User user) { //update by id } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public void delete(@PathVariable("id") Long id) { //delete by id }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接