测设类中心启动Web项目

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);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值