单元测试模拟request后台
编写测试单元
@RunWith(SpringJUnit4ClassRunner.class) 让测试运行于Spring测试环境
@WebAppConfiguration是一个类级别的注释,用于声明ApplicationContext为集成测试加载的应该是一个WebAppConfiguration。
@ContextConfiguration(locations={" "," "}) Spring整合JUnit4测试时,使用注解引入多个配置文件
1.编写controller类
@Controller
public class BasicMsgContrller {
@Autowired
BasicService basicService;
@RequestMapping("alltest")
public void getAllStuInfo(@RequestParam(value="a",defaultValue="1") Integer i,Model m){
//函数来指定 pageNum(第几页) 和 pageSize(每页显示几条记录) 两个参数。
PageHelper.startPage(i, 5);
List <Sbasicmessage> allStu = basicService.getallStu();
//使用pageInfo包装查询后的结果,封装了详细的查询数据,
PageInfo page=new PageInfo(allStu);
m.addAttribute("pageInfo", page);
System.out.println("总数:"+page.getTotal()); }
2.测试类
//让测试运行于Spring测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//web请求容器
@WebAppConfiguration
//@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件
@ContextConfiguration(locations={"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/springmvc-servlet.xml"})
public class MockMvcTest { @Autowired
WebApplicationContext context;
//模拟请求
MockMvc mvc;
@Before
public void initMockMvc(){ mvc=MockMvcBuilders.webAppContextSetup(context).build(); } @Test
public void testpage() throws Exception{
//发起一次请求 andReturn返回的结果
MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get("/alltest"). param("a","1")).andReturn();
MockHttpServletRequest mockrequest= mvcResult.getRequest();
PageInfo pageinfo=(PageInfo)mockrequest.getAttribute("pageInfo");
System.out.println("当前总页数为"+pageinfo.getTotal());
}
单元测试模拟request后台的更多相关文章
- flask设置cookie,设置session,模拟用户认证、模拟管理后台admin、模拟用户logout
设置cookie HTTP协议是无状态的,在一次请求响应结束后,服务器不会留下关于客户端状态的信息.但是对于某些web程序来说,客户端的信息有必要被记住,比如用户的登录状态,这样就可以根据用户的状态来 ...
- Postman模拟Request Payload发送请求
Postman模拟Request Payload发送请求,如下图所示:
- unity, 模拟退后台
//simulateSwitchToBackground.cs using UnityEngine;using System.Collections;using System.Collections. ...
- (一)微信小程序之模拟调用后台接口踩过的坑
如下图标记的三个点 在调试过程中出现问题,特此记录. 1. 之前在浏览器测试接口习惯省略 http:// ,是因为浏览器默认有一个检测,在你输入的网址前面加http://,如果有就不加. 然而在微信小 ...
- springBoot单元测试-模拟MVC测试
1)模拟mvc测试,和基础测试是一样的, 都需要在pom文件中引入junit的支持. 略 2)编写测试类 Application1TestMVC 在类头上除啦加入之前的@RunWith(SpringR ...
- springboot的junit4模拟request、response对象
关键字: MockHttpRequest.Mock测试 问题: 在模拟junit的request.response对象时,会报如下空指针异常. 处理方法: 可用MockHttpServletReque ...
- Laravel 单元测试-模拟认证的用户
在 Laravel 编写单元测试时经常会遇到需要模拟认证用户的时候,比如新建文章.创建订单等,那么在 Laravel unit test 中如何来实现呢? 官方解决方法 Laravel 的官方文档中的 ...
- angular js 模拟获取后台的数据
在这里我们把后台的数据用一个.json文件进行代替. 项目的目录结构如下: puDongLibraryLearning----ui-router-learning ---- data-------pe ...
- Ajax的post方法,模拟 从后台读取数据小demo
$(document).ready(function() { //定义一个函数 function timer() { $.post("1.json", function(data, ...
随机推荐
- D - Back and Forth(模拟)
Problem Statement Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis point ...
- 深入浅出git
图文 http://www.cnblogs.com/syp172654682/p/7689328.html 廖雪峰 https://www.liaoxuefeng.com/wiki/001373951 ...
- loj #2044. 「CQOI2016」手机号码
#2044. 「CQOI2016」手机号码 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- luogu4449 于神之怒加强版(莫比乌斯反演)
link 给定n,m,k,计算\(\sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^k\)对1000000007取模的结果 多组数据,T<=2000,1<=N,M,K&l ...
- An error has occurred. See error log for more details. java.lang.NullPointerException
今天重新新建一个eclipse的空间,就报了这么一个错误,百度找了很多,发现不对头,都不行.后来点开错误一看,原来是web.xml里面没有文件头造成的. 解决办法:只需要在web.xml文件里添加文件 ...
- 19.Longest Substring Without Repeating Characters(长度最长的不重复子串)
Level: Medium 题目描述: Given a string, find the length of the longest substring without repeating cha ...
- 2018北京网络赛D 80days (尺取)
#1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...
- centos7上面关闭防火墙
CentOS 7.0默认使用的是firewall作为防火墙:若没有启用iptables 作为防火墙,则使用以下方式关闭防火墙: systemctl stop firewalld.service 关闭开 ...
- react PropTypes 与 DefaultProps
PropTypes 与 DefaultProps import React ,{ Component } from 'react'; import PropTypes from 'prop-types ...
- js 时间日期大小对比
var oDate1 = new Date(); var oDate2 = new Date("2019/01/07 10:00:00"); if (oDate1.getTime( ...