springboot测试工具mock使用demo

测试对应的controller:

package com.special.weixin.weixindev.controller;

import org.omg.PortableInterceptor.SUCCESSFUL;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Source;

/**
 * @author liuYC
 * @ClassName TestMockController
 * @Description TODO
 * @date 2021/11/10 10:50
 */
@RestController
@RequestMapping("/")
public class TestMockController {
    @RequestMapping("testMock")
    public String test(@RequestParam String s) {
        System.out.println("----------------------test------------------------");
        System.out.println("hello mock is succcess!");
        return s;
    }
}

测试结果如下:

 

测试通过,在测试对应的mock进行构建测试的方法:

package com.special.weixin.springboot.testspeingboot;

import com.special.weixin.WeixinApplication;
import com.special.weixin.springboot.MainConfig;
import com.special.weixin.springboot.bean.Person;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author liuYC
 * @ClassName Test
 * @Description TODO
 * @date 2021/11/9 16:13
 */
//SpringBoot1.4版本之前用的是SpringJUnit4ClassRunner.class
@RunWith(SpringRunner.class)
//SpringBoot1.4版本之前用的是@SpringApplicationConfiguration(classes = Application.class)
@SpringBootTest(classes = WeixinApplication.class)
//测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的
@WebAppConfiguration
public class MockTest {

    @Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception{
        //MockMvcBuilders.webAppContextSetup(WebApplicationContext context):指定WebApplicationContext,将会从该上下文获取相应的控制器并得到相应的MockMvc;
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();//建议使用这种
    }

    @Test
    public void getHello() throws Exception{

        /**
         * 1、mockMvc.perform执行一个请求。
         * 2、MockMvcRequestBuilders.get("XXX")构造一个请求。
         * 3、ResultActions.param添加请求传值
         * 4、ResultActions.accept(MediaType.TEXT_HTML_VALUE))设置返回类型
         * 5、ResultActions.andExpect添加执行完成后的断言。
         * 6、ResultActions.andDo添加一个结果处理器,表示要对结果做点什么事情
         *   比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
         * 5、ResultActions.andReturn表示执行完成后返回相应的结果。
         */

        MvcResult mvcResult= mockMvc.perform(MockMvcRequestBuilders.get("/testMock")
                .param("s","its success !!!!!")
                .accept(MediaType.parseMediaType("text/plain;charset=UTF-8")))
                // .andExpect(MockMvcResultMatchers.status().isOk())             //等同于Assert.assertEquals(200,status);
                // .andExpect(MockMvcResultMatchers.content().string("hello lvgang"))    //等同于 Assert.assertEquals("hello lvgang",content);
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        int status=mvcResult.getResponse().getStatus();                 //得到返回代码
        String content=mvcResult.getResponse().getContentAsString();    //得到返回结果

        Assert.assertEquals(200,status);                        //断言,判断返回代码是否正确
        Assert.assertEquals("its success !!!!!",content);            //断言,判断返回的值是否正确
    }

}

其中 SpringBootApplication 为对应的spring boot的启动内的入口函数

有关mock使用的详细原理解释和从中的代码设计理念学习链接如下

有关mock使用的详细原理解释和从中的代码设计理念学习链接如下_liudaka的博客-CSDN博客有关mock使用的详细原理解释和从中的代码设计理念学习链接如下https://blog.csdn.net/liudaka/article/details/121245054

有关Assert的实现原理和对应的使用情况,链接如下:

有关Assert的实现原理和对应的使用情况_liudaka的博客-CSDN博客有关如何使用的的问题的接下来需要进行的原理的解释,和对应的可以从中借鉴得到什么!https://blog.csdn.net/liudaka/article/details/121245003

参考的官方文档中文版:

 

参考文档:

SpringBoot 使用MockMvc进行Controller的测_JobShow裁员加班实况-微信小程序-CSDN博客_mockmvcrequestbuilders

 参考的 spring framework 

关注公众号:

 

回复 spring-framework  获取 spring-framework.pdf官方文档中文版

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

specialApe

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

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

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

打赏作者

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

抵扣说明:

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

余额充值