1. 单元测试需添加标签,表示单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
2.在测试的方法上加标签
@Test
3.测试 Service
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.domain.service.IUserInfoService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TheSardineMerveilleApplicationTests {
@Autowired
private IUserInfoService iUserInfoService;
@Test
public void user() throws Exception {
iUserInfoService.getUserById(917);
}
}
4.测试Controller 需要加类标签
@AutoConfigureWebMvc
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
@Autowired
/* 测试 controller */
private MockMvc mvc;
@Test
public void contextLoads() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/url").contentType(MediaType.APPLICATION_JSON_UTF8))
// 判断返回状态 200 404
.andExpect(MockMvcResultMatchers.status().isOk())
// 返回值等于 什么
.andExpect(MockMvcResultMatchers.content().string("返回值"));
}
}
5.测试Controller 携带参数
(1). 单个参数传递
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
public void setUp(){
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void user() throws Exception {
setUp();
mvc.perform(MockMvcRequestBuilders.get("/url")
// 传递参数
.param(key, value))
// 判断返回状态 200 404
.andExpect(MockMvcResultMatchers.status().isOk())
// 返回值等于 什么
.andExpect(MockMvcResultMatchers.content().string("返回值"));
}
}
/*
* required 是否可为空
* defaultValue 默认值
*/
@RequestMapping("/url")
public ResultVO<UserInfo> user(@RequestParam(name="key", required=false, defaultValue="value") String key){
System.out.println(key);
}
(2).对象传值
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
public void setUp(){
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void user() throws Exception {
setUp();
mvc.perform(MockMvcRequestBuilders.get("/url")
// 传递参数
.param(key, value))
.param(key, value))
.param(key, value))
.param(key, value))
.param(key, value))
.param(key, value))
// 判断返回状态 200 404
.andExpect(MockMvcResultMatchers.status().isOk())
// 返回值等于 什么
.andExpect(MockMvcResultMatchers.content().string("返回值"));
}
}
@RequestMapping("/user")
public List<UserInfo> user(UserInfo userInfo) throws ExceptionHandleMes {
System.out.println(userInfo);
return null;
}
6.分页 及排序
Pageable
需要有 size number sort
package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
public void setUp(){
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void user() throws Exception {
setUp();
mvc.perform(MockMvcRequestBuilders.get("/url")
// 传递参数
.param(key, value))
.param(key, value))
.param(key, value))
.param("number", 5))
.param("sort", "age,desc"))
// 判断返回状态 200 404
.andExpect(MockMvcResultMatchers.status().isOk())
// 返回值等于 什么
.andExpect(MockMvcResultMatchers.content().string("返回值"));
}
}
/***
* @PageableDefault 默认分页信息
*
*/
@RequestMapping("/user")
public List<UserInfo> user(UserInfo userInfo, @PageableDefault(page=1, size=5, sort="user_id desc") Pageable pageable){
System.out.println(userInfo);
System.out.println(pageable);
return null;
}
注:单元测试在程序打包的时候会自动进行。
执行下面命令跳过单元测试
mvn clean package -Dmaven.test.skip=true
如果 MockMvc 注册失败,则使用第二种方法,使用时 先注册
@Autowired
/* 测试 controller */
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
public void setUp(){
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}