springboot之mockMVC测试Controller

MockMvc简介

MockMvc实现对Http请求的模拟,可以方便对Controller进行测试,使得测试速度快、不依赖网络环境,而且提供验证的工具,使得请求的验证统一而且很方便

测试环境

jdk 1.8
springboot 2.x
junit4

导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
</dependency>

测试代码


import org.junit.Before;
import org.junit.jupiter.api.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.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;



//SpringBoot1.4版本之前用的是SpringJUnit4ClassRunner.class
@RunWith(SpringRunner.class)
//SpringBoot1.4版本之前用的是@SpringApplicationConfiguration(classes = Application.class)
@SpringBootTest
//测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的
@WebAppConfiguration
//@AutoConfigureMockMvc是用于自动配置MockMvc
@AutoConfigureMockMvc
class UserControllerTest {
    @Autowired
    private WebApplicationContext webApplicationContext;
    @Autowired
    private MockMvc mockMvc;


    @Before()
    public void setup(){
        mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();//建议使用这种
    }

    @Test
    public void addUser() throws Exception{

        MvcResult response = mockMvc.perform(MockMvcRequestBuilders.post("/user/adduser")
                .contentType(MediaType.APPLICATION_JSON).param("userAccount","killer").param("userPassword",
                        "123456").param("userTel","133xxxx6657").param("userImage","logo").param("userName","jack").param(
                        "userNickname","jeff").param("userAcademy","失乐院").param("userClass","石乐志3班").param(
                        "userStudentNumber","2019212217890").param("userEmail","shilezhi@example.com").param("gender","男")
                .param("classId","2")).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();
        System.out.println("result:"+response.getResponse().getContentAsString());
    }
}

对mockMvc中一些方法的使用描述。
perform() 发送请求,获取请求结果里面的参数可以选择post|get|put|delete
post()里面的“/user/adduser"即为controller接口的对应url。
contentType()中选择数据的传输方式,这里采用的是JSON格式。
prama()中传输的就是键值对,前面为controller接收参数的名称,后面是传输的值。
andExpect()期望浏览器返回的状态码。这里期望返回200。
andDo(print())将返回信息打印出来。
getRespnse().getcontentAsString()请求返回的值。
返回值图
返回这样即为成功。

必坑指南

网上的一些注解被弃用,测试的注解就按博主的来最好,实体解析为JSON存在问题。(本人是手打的好多prama,后期如果尝试其他好用的会回来更新滴)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值