SpringBoot测试用例
@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<User> findAll() {
List<User> 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;
}
}
编写测试类
新建一个测试父类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则测试该包下所有测试类
来源:CSDN
作者:l_learning
链接:https://blog.csdn.net/l_learning/article/details/103569814