package com.lele;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lele.dao.CustomerDao;
import com.lele.domain.Customer;
import org.junit.jupiter.api.Test;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.*;
import java.text.SimpleDateFormat;
import java.util.Date;
//properties可以为当前的测试用例添加临时的属性
//@SpringBootTest(properties = {""})
//args属性可以为当前测试用例添加临时的命令参数
//@SpringBootTest(args = {"--",""})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
class SpringBootMybatisPlusVueHouseApplicationTests {
@Autowired
private CustomerDao customerDao;
// @Autowired
// private MockMvc mockMvc;
@Test
void contextLoads() {
LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<Customer>();
//等同
wrapper.eq(Customer::getUsername, "admin");
Customer res = customerDao.selectOne(wrapper);
System.out.println(res);
}
@Test
void dataTest() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String str = sdf.format(date);
System.out.println(str);
}
@Test
void testWeb(@Autowired MockMvc mvc) throws Exception {
//创建了一个虚拟的请求
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");
ResultActions perform = mvc.perform(builder);
//设置预期值 与真实值进行比较 成功通过测试 失败测试失败
//定义本次调用的预期值
StatusResultMatchers status = MockMvcResultMatchers.status();
//预计本次调用时成功的:状态200
ResultMatcher ok = status.isOk();
//添加预计值到本调用过程中进行匹配
perform.andExpect(ok);
}
@Test
void testBody(@Autowired MockMvc mvc) throws Exception {
//创建了一个虚拟的请求
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("");
ResultActions perform = mvc.perform(builder);
//设置预期值 与真实值进行比较 成功通过测试 失败测试失败
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用时成功的:状态200
ResultMatcher result = content.string("");
//添加预计值到本调用过程中进行匹配
perform.andExpect(result);
}
@Test
void testJson(@Autowired MockMvc mvc) throws Exception {
//创建了一个虚拟的请求
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("");
ResultActions perform = mvc.perform(builder);
//设置预期值 与真实值进行比较 成功通过测试 失败测试失败
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用时成功的:状态200
ResultMatcher result = content.json("");
//添加预计值到本调用过程中进行匹配
perform.andExpect(result);
}
@Test
void testContentType(@Autowired MockMvc mvc) throws Exception {
//创建了一个虚拟的请求
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("");
ResultActions perform = mvc.perform(builder);
//设置预期值 与真实值进行比较 成功通过测试 失败测试失败
//定义本次调用的预期值
HeaderResultMatchers header = MockMvcResultMatchers.header();
//预计本次调用时成功的:状态200
ResultMatcher string = header.string("", "");
//添加预计值到本调用过程中进行匹配
perform.andExpect(string);
}
@Test
void testALL(@Autowired MockMvc mvc) throws Exception {
//创建了一个虚拟的请求
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("");
ResultActions perform = mvc.perform(builder);
//设置预期值 与真实值进行比较 成功通过测试 失败测试失败
//定义本次调用的预期值
//定义本次调用的预期值
StatusResultMatchers status = MockMvcResultMatchers.status();
//预计本次调用时成功的:状态200
ResultMatcher ok = status.isOk();
//添加预计值到本调用过程中进行匹配
perform.andExpect(ok);
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计本次调用时成功的:状态200
ResultMatcher result = content.json("");
//添加预计值到本调用过程中进行匹配
perform.andExpect(result);
HeaderResultMatchers header = MockMvcResultMatchers.header();
//预计本次调用时成功的:状态200
ResultMatcher string = header.string("", "");
//添加预计值到本调用过程中进行匹配
perform.andExpect(string);
}
}