一:介紹說明
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指定分頁參數默認值
2.@RestController與@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.效果
說明,服務已經建立起來了。
4.@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 .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 }
效果
6.@Pageable
測試類
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 }
效果