SpringBoot测试用例
- 搭建一个简单Spring Boot的被测试环境
- 编写测试类
- 测试结果
@RunWith(SpringRunner.class)
如何运行这个测试类,这里用SpringRunner运行测试用例
@SpringBootTest
代表这是一个测试类
@Test
测试方法
WEB项目需一个MVC环境
搭建一个简单Spring Boot的被测试环境 新建一个User对象
package com.user.domain;
public class User {
private String id;
private String name;
private Integer age;
public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}}
web层
package com.user.controll;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.user.domain.User;
@RestController
@RequestMapping("/user")
public class UserControll {
@GetMapping
public List findAll() {
List users = new ArrayList<>();
User user = new User();
user.setId("1");
user.setName("zhangsan");
user.setAge(18);
users.add(user);
return users;
}
@GetMapping("/{id}")
public User getUserInfo(String id) {
User user = new User();
user.setId("2");
user.setName("lisi");
user.setAge(18);
return user;
}}
编写测试类 【JAVA基础|SpringBoot测试用例】新建一个测试父类TestParent
package com.user;
import org.junit.Before;
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 org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestParent {
@Autowired
protected WebApplicationContext wac;
//MVC环境
protected MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}}
UserControllTest 测试类
package com.user.controll;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.user.TestParent;
public class UserControllTest extends TestParent {
@Test
public void whenQuerySuccess() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user")
.param("username", "zhangsan")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(1));
}
@Test
public void whenGetInfoSuccess() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/2")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("lisi"));
}
}
测试结果 方法上Run As -> JUnit Test则测试方法
类空白处上Run As -> JUnit Test则测试该测试类的所有@Test标注的方法
包上Run As -> JUnit Test则测试该包下所有测试类
文章图片
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])