博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMvc-MockMvc
阅读量:4304 次
发布时间:2019-05-27

本文共 2596 字,大约阅读时间需要 8 分钟。

1.写一个父类package 你的包;import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;import java.nio.charset.Charset;import org.junit.Before;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.web.servlet.MockMvc;import org.springframework.web.context.WebApplicationContext;@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringTestCase extends AbstractJUnit4SpringContextTests{	public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));    @Autowired    protected WebApplicationContext wac;    protected MockMvc mockMvc;    @Before    public void setup() throws Exception {        this.mockMvc = webAppContextSetup(this.wac).build();    }}2.测试类继承上边的类,下面是一个测试登录的集成测试,模拟发送HTTP post请求package 包名;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;import org.junit.Assert;import org.junit.Test;import org.springframework.http.MediaType;import org.springframework.test.web.servlet.MvcResult;import com.alibaba.fastjson.JSONObject;public class LoginTest2 extends SpringTestCase{	@Test	public void test() throws Exception {		JSONObject param = new JSONObject();		param.put("mobile", "16156536563");		param.put("password", "mima");		String actual = param.toJSONString();		System.out.println("============param===>" + actual);		MvcResult mvcResult = this.mockMvc				.perform(						post("/login")	                       .characterEncoding("UTF-8")  	                       .contentType(MediaType.APPLICATION_JSON)	                       .content(actual)								.header("deviceId", "bb")	            		)			            .andDo(print()).andReturn();				int status = mvcResult.getResponse().getStatus();		Assert.assertEquals(200, status);  		String result = mvcResult.getResponse().getContentAsString();  		JSONObject jsonObject = JSONObject.parseObject(result);  		Assert.assertEquals(true, jsonObject.containsKey("result"));		Assert.assertEquals(true, jsonObject.containsKey("resultCode"));		Assert.assertEquals(true, jsonObject.containsKey("resultMessage"));		Assert.assertEquals(true, jsonObject.containsKey("data"));	}	}

转载地址:http://mvhws.baihongyu.com/

你可能感兴趣的文章
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>
suse创建的虚拟机,修改ip地址
查看>>
linux的挂载的问题,重启后就挂载就没有了
查看>>
docker原始镜像启动容器并创建Apache服务器实现反向代理
查看>>
docker容器秒死的解决办法
查看>>
管理网&业务网的一些笔记
查看>>
openstack报错解决一
查看>>
openstack报错解决二
查看>>
linux source命令
查看>>
openstack报错解决三
查看>>
乙未年年终总结
查看>>
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>