栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot测试工具mock使用demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


@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;


//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{

        

        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官方文档中文版

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号