一:介绍说明
1.介绍
2.restful api的成熟度
二:编写Restful API的测试用例
1.引入spring的测试框架
在effective pom中查找
2.新建测试包,测试类
3.测试用例程序
1 package com.cao.web.controller; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.http.MediaType; 9 import org.springframework.test.context.junit4.SpringRunner; 10 import org.springframework.test.web.servlet.MockMvc; 11 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 12 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 13 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 import org.springframework.web.context.WebApplicationContext; 15 16 //如何运行测试用例 17 @RunWith(SpringRunner.class) 18 //这是一个测试用例 19 @SpringBootTest 20 public class UserControllerTest { 21 //伪造测试用例,不需要跑tomcat,运行会很快 22 @Autowired 23 private WebApplicationContext wac; 24 25 //伪造的一个mvc环境 26 private MockMvc mockMvc; 27 28 @Before 29 public void setup() { 30 //初始化这个环境 31 mockMvc=MockMvcBuilders.webAppContextSetup(wac).build(); 32 } 33 34 @Test 35 public void whenQuerySuccess() throws Exception { 36 //发送请求 37 mockMvc.perform(MockMvcRequestBuilders.get("/user") 38 .contentType(MediaType.APPLICATION_JSON_UTF8)) 39 .andExpect(MockMvcResultMatchers.status().isOk()) 40 .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)); 41 } 46 47 }
4.执行效果
三:使用注解声明RestfulAPI
1.常用注解
@RestController标明此controller提供RestAPI
@RequestMapping及其变体,映射url到java
@RequestParam映射请求参数到java方法上的参数、
@PageableDefault指定分页参数默认值
[email protected]与@RequestMapping小测试
控制类
1 package com.cao.web.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import com.cao.dto.User; 11 12 //此controller可以提供restful服务 13 @RestController 14 public class UserController { 15 @RequestMapping(value="/user",method=RequestMethod.GET) 16 public List<User> query(){ 17 List<User> userList=new ArrayList<>(); 18 userList.add(new User()); 19 userList.add(new User()); 20 userList.add(new User()); 21 return userList; 22 } 23 24 }
User.java
新建dto包
1 package com.cao.dto; 2 3 public class User { 4 private String username; 5 private String password; 6 7 public String getUsername() { 8 return username; 9 } 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 public String getPassword() { 14 return password; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 20 }
3.效果
说明,服务已经建立起来了。
[email protected]小测试【单个参数】
常规的用法
测试类
1 package com.cao.web.controller; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.http.MediaType; 9 import org.springframework.test.context.junit4.SpringRunner; 10 import org.springframework.test.web.servlet.MockMvc; 11 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 12 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 13 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 import org.springframework.web.context.WebApplicationContext; 15 16 //如何运行测试用例 17 @RunWith(SpringRunner.class) 18 //这是一个测试用例 19 @SpringBootTest 20 public class UserControllerTest { 21 //伪造测试用例,不需要跑tomcat,运行会很快 22 @Autowired 23 private WebApplicationContext wac; 24 25 //伪造的一个mvc环境 26 private MockMvc mockMvc; 27 28 @Before 29 public void setup() { 30 //初始化这个环境 31 mockMvc=MockMvcBuilders.webAppContextSetup(wac).build(); 32 } 33 34 @Test 35 public void whenQuerySuccess() throws Exception { 36 //发送请求 37 mockMvc.perform(MockMvcRequestBuilders.get("/user") 38 .param("username", "Job") 39 .contentType(MediaType.APPLICATION_JSON_UTF8)) 40 .andExpect(MockMvcResultMatchers.status().isOk()) 41 .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)); 42 } 43 44 45 }
控制类
1 package com.cao.web.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RequestParam; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import com.cao.dto.User; 12 13 //此controller可以提供restful服务 14 @RestController 15 public class UserController { 16 @RequestMapping(value="/user",method=RequestMethod.GET) 17 public List<User> query(@RequestParam String username){ 18 List<User> userList=new ArrayList<>(); 19 System.out.println("username="+username); 20 userList.add(new User()); 21 userList.add(new User()); 22 userList.add(new User()); 23 return userList; 24 } 25 26 }
RequestParam的其他参数
1 package com.cao.web.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RequestParam; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import com.cao.dto.User; 12 13 //此controller可以提供restful服务 14 @RestController 15 public class UserController { 16 @RequestMapping(value="/user",method=RequestMethod.GET) 17 public List<User> query(@RequestParam(name="username",required=false,defaultValue="Tom") String name){ 18 List<User> userList=new ArrayList<>(); 19 System.out.println("name="+name); 20 userList.add(new User()); 21 userList.add(new User()); 22 userList.add(new User()); 23 return userList; 24 } 25 26 }
这里主要是要注意一下别名。
5.使用类组装多个参数【多个参数进行传递】
这里主要是说上面的RequestParam不再满足的时候,主要的场景是复杂的多请求参数时
测试类
1 package com.cao.web.controller; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.http.MediaType; 9 import org.springframework.test.context.junit4.SpringRunner; 10 import org.springframework.test.web.servlet.MockMvc; 11 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 12 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 13 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 import org.springframework.web.context.WebApplicationContext; 15 16 //如何运行测试用例 17 @RunWith(SpringRunner.class) 18 //这是一个测试用例 19 @SpringBootTest 20 public class UserControllerTest { 21 //伪造测试用例,不需要跑tomcat,运行会很快 22 @Autowired 23 private WebApplicationContext wac; 24 25 //伪造的一个mvc环境 26 private MockMvc mockMvc; 27 28 @Before 29 public void setup() { 30 //初始化这个环境 31 mockMvc=MockMvcBuilders.webAppContextSetup(wac).build(); 32 } 33 34 @Test 35 public void whenQuerySuccess() throws Exception { 36 //发送请求 37 mockMvc.perform(MockMvcRequestBuilders.get("/user") 38 .param("username", "Job") 39 .param("age", "18") 40 .param("xxx", "XXX") 41 .contentType(MediaType.APPLICATION_JSON_UTF8)) 42 .andExpect(MockMvcResultMatchers.status().isOk()) 43 .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)); 44 } 45 46 47 }
控制类
1 package com.cao.web.controller; 2 3 import static org.mockito.Matchers.contains; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.apache.commons.lang.builder.ReflectionToStringBuilder; 9 import org.apache.commons.lang.builder.ToStringStyle; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.RequestParam; 13 import org.springframework.web.bind.annotation.RestController; 14 15 import com.cao.dto.User; 16 import com.cao.dto.UserQueryCondition; 17 18 //此controller可以提供restful服务 19 @RestController 20 public class UserController { 21 @RequestMapping(value="/user",method=RequestMethod.GET) 22 public List<User> query(UserQueryCondition condition){ 23 //反射的方法来打印 24 System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE)); 25 // 26 List<User> userList=new ArrayList<>(); 27 userList.add(new User()); 28 userList.add(new User()); 29 userList.add(new User()); 30 return userList; 31 } 32 33 }
UserQueryCondition.java
1 package com.cao.dto; 2 3 public class UserQueryCondition { 4 private String username; 5 private int age; 6 private int ageTo; 7 private String xxx; 8 public String getUsername() { 9 return username; 10 } 11 public void setUsername(String username) { 12 this.username = username; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 public int getAgeTo() { 21 return ageTo; 22 } 23 public void setAgeTo(int ageTo) { 24 this.ageTo = ageTo; 25 } 26 public String getXxx() { 27 return xxx; 28 } 29 public void setXxx(String xxx) { 30 this.xxx = xxx; 31 } 32 33 }
效果
[email protected]
测试类
1 package com.cao.web.controller; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.http.MediaType; 9 import org.springframework.test.context.junit4.SpringRunner; 10 import org.springframework.test.web.servlet.MockMvc; 11 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 12 import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 13 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 import org.springframework.web.context.WebApplicationContext; 15 16 //如何运行测试用例 17 @RunWith(SpringRunner.class) 18 //这是一个测试用例 19 @SpringBootTest 20 public class UserControllerTest { 21 //伪造测试用例,不需要跑tomcat,运行会很快 22 @Autowired 23 private WebApplicationContext wac; 24 25 //伪造的一个mvc环境 26 private MockMvc mockMvc; 27 28 @Before 29 public void setup() { 30 //初始化这个环境 31 mockMvc=MockMvcBuilders.webAppContextSetup(wac).build(); 32 } 33 34 @Test 35 public void whenQuerySuccess() throws Exception { 36 //发送请求 37 mockMvc.perform(MockMvcRequestBuilders.get("/user") 38 .param("username", "Job") 39 .param("age", "18") 40 .param("xxx", "XXX") 41 //分页,查第三页,每页15条,按照age降序 42 .param("page", "3") 43 .param("size", "15") 44 .param("sort", "age,desc") 45 // 46 .contentType(MediaType.APPLICATION_JSON_UTF8)) 47 .andExpect(MockMvcResultMatchers.status().isOk()) 48 .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)); 49 } 50 51 52 }
控制类
1 package com.cao.web.controller; 2 3 import static org.mockito.Matchers.contains; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.apache.commons.lang.builder.ReflectionToStringBuilder; 9 import org.apache.commons.lang.builder.ToStringStyle; 10 import org.springframework.data.domain.Pageable; 11 import org.springframework.data.web.PageableDefault; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestMethod; 14 import org.springframework.web.bind.annotation.RequestParam; 15 import org.springframework.web.bind.annotation.RestController; 16 17 import com.cao.dto.User; 18 import com.cao.dto.UserQueryCondition; 19 20 //此controller可以提供restful服务 21 @RestController 22 public class UserController { 23 @RequestMapping(value="/user",method=RequestMethod.GET) 24 public List<User> query(UserQueryCondition condition,@PageableDefault(size=13) Pageable pageable){ 25 //反射的方法来打印 26 System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE)); 27 // 28 System.out.println(pageable.getPageSize()); 29 System.out.println(pageable.getPageNumber()); 30 System.out.println(pageable.getSort()); 31 // 32 List<User> userList=new ArrayList<>(); 33 userList.add(new User()); 34 userList.add(new User()); 35 userList.add(new User()); 36 return userList; 37 } 38 39 }
效果
原文地址:https://www.cnblogs.com/juncaoit/p/9697096.html
时间: 2024-10-28 15:27:29