概述
参考:https://www.jianshu.com/p/81fc2c98774f
Spring基本单元测试
- pom.xml 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 在测试目录添加测试类
1.注入服务直接测试
@RunWith(SpringRunner.class)
@SpringBootTest
//只注入依赖的bean,加快测试速度
//@ContextConfiguration(classes = {
// AccountController.class,
// SysLoginService.class,
// SysLoginFallBack.class
//})
public class AccountControllerTest {
@Autowired
SysLoginService sysLoginService;
@Test
public void testLogin(){
LoginDTO loginDTO = new LoginDTO();
loginDTO.setLoginType(LoginType.mobile);
loginDTO.setAccount("888888");
loginDTO.setPassword("123456");
ReturnEntity returnEntity = sysLoginService.Login(loginDTO);
//断言:测试返回实体不为null
Assert.assertNotNull(returnEntity);
//断言:测试接口状态成功与否
Assert.assertEquals("0000" , returnEntity.getResultCode());
//查看token
System.out.println(returnEntity.getResultObj());
}
}
2.使用MockMvc进行测试
import com.alibaba.fastjson.JSON;
import com.egovchina.partybuilding.common.enums.LoginType;
import com.egovchina.partybuilding.common.pojo.dto.LoginDTO;
import com.egovchina.partybuilding.common.util.ReturnEntity;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc//配置mock
public class AccountControllerMockTest {
@Autowired
private MockMvc mvc;
@Test
public void testLogin() throws Exception{
LoginDTO loginDTO = new LoginDTO();
loginDTO.setLoginType(LoginType.mobile);
loginDTO.setAccount("888888");
loginDTO.setPassword("123456");
String requestUrl = "/users/login";
MvcResult result = mvc.perform(
MockMvcRequestBuilders.post(requestUrl)
.content(JSON.toJSONString(loginDTO))
.contentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8_VALUE)
.accept(MediaType.APPLICATION_PROBLEM_JSON_UTF8_VALUE)
)
.andExpect(status().isOk())
.andDo(print())
.andReturn();
String content = result.getResponse().getContentAsString();
//测试返回实体不为null
Assert.assertNotNull(content);
ReturnEntity returnEntity = JSON.parseObject(content , ReturnEntity.class);
//测试返回实体不为null
Assert.assertNotNull(returnEntity);
//测试接口状态成功与否
Assert.assertEquals("0000" , returnEntity.getResultCode());
//查看token
System.out.println(returnEntity.getResultObj());
}
}
还可以在pom.xml中配置启动项目跳过测试
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 启动项目跳过测试 -->
<maven.test.skip>true</maven.test.skip>
</properties>
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter21ApplicationTests {
private MockMvc mvc;
@Before
public void setUp() {
mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
}
@Test
public void testUserController() throws Exception {
// 测试UserController
RequestBuilder request;
// 1、get查一下user列表,应该为空
request = get("/users/");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[]")));
// 2、post提交一个user
request = post("/users/")
.contentType(MediaType.APPLICATION_JSON)
.content("{"id":1,"name":"测试大师","age":20}");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));
// 3、get获取user列表,应该有刚才插入的数据
request = get("/users/");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[{"id":1,"name":"测试大师","age":20}]")));
// 4、put修改id为1的user
request = put("/users/1")
.contentType(MediaType.APPLICATION_JSON)
.content("{"name":"测试终极大师","age":30}");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));
// 5、get一个id为1的user
request = get("/users/1");
mvc.perform(request)
.andExpect(content().string(equalTo("{"id":1,"name":"测试终极大师","age":30}")));
// 6、del删除id为1的user
request = delete("/users/1");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));
// 7、get查一下user列表,应该为空
request = get("/users/");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[]")));
}
}
最后
以上就是合适钢笔为你收集整理的SpringBoot集成单元测试的全部内容,希望文章能够帮你解决SpringBoot集成单元测试所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复