编写测试单元

    @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后台的更多相关文章

  1. flask设置cookie,设置session,模拟用户认证、模拟管理后台admin、模拟用户logout

    设置cookie HTTP协议是无状态的,在一次请求响应结束后,服务器不会留下关于客户端状态的信息.但是对于某些web程序来说,客户端的信息有必要被记住,比如用户的登录状态,这样就可以根据用户的状态来 ...

  2. Postman模拟Request Payload发送请求

    Postman模拟Request Payload发送请求,如下图所示:

  3. unity, 模拟退后台

    //simulateSwitchToBackground.cs using UnityEngine;using System.Collections;using System.Collections. ...

  4. (一)微信小程序之模拟调用后台接口踩过的坑

    如下图标记的三个点 在调试过程中出现问题,特此记录. 1. 之前在浏览器测试接口习惯省略 http:// ,是因为浏览器默认有一个检测,在你输入的网址前面加http://,如果有就不加. 然而在微信小 ...

  5. springBoot单元测试-模拟MVC测试

    1)模拟mvc测试,和基础测试是一样的, 都需要在pom文件中引入junit的支持. 略 2)编写测试类 Application1TestMVC 在类头上除啦加入之前的@RunWith(SpringR ...

  6. springboot的junit4模拟request、response对象

    关键字: MockHttpRequest.Mock测试 问题: 在模拟junit的request.response对象时,会报如下空指针异常. 处理方法: 可用MockHttpServletReque ...

  7. Laravel 单元测试-模拟认证的用户

    在 Laravel 编写单元测试时经常会遇到需要模拟认证用户的时候,比如新建文章.创建订单等,那么在 Laravel unit test 中如何来实现呢? 官方解决方法 Laravel 的官方文档中的 ...

  8. angular js 模拟获取后台的数据

    在这里我们把后台的数据用一个.json文件进行代替. 项目的目录结构如下: puDongLibraryLearning----ui-router-learning ---- data-------pe ...

  9. Ajax的post方法,模拟 从后台读取数据小demo

    $(document).ready(function() { //定义一个函数 function timer() { $.post("1.json", function(data, ...

随机推荐

  1. 从零开始安装 Ambari (3) -- 安装 Ambari

    1. 安装 yum -y install ambari-server 2. ambari server 需要一个数据库存储元数据,默认使用的 Postgres 数据库.默认的用户名和密码是: amba ...

  2. 【谁知道C#字段为什么用属性封装?】

    源地址:https://zhidao.baidu.com/question/1174413218458798139.html 我们知道,类成员包括变量和方法.如果希望其他类能够访问成员变量的值,就必须 ...

  3. chrome插件-YSlow 一个使用的web性能测试插件

    本文为转载是文章,如作者发现后不愿意,请联系我进行删除 原文链接:http://www.cnblogs.com/wajika/p/6278825.html YSlow的安装: 1.安装 firebug ...

  4. P2264 情书 Trie匹配

    \(\color{#0066ff}{题目描述}\) 为了帮助CYY,我们定义一个量化情书好坏的标准感动值.判断感动值的方法如下: 1.在情书的一句话中若含有给定词汇列表中的特定单词,则感动值加1,但每 ...

  5. 使用box-shadow 实现水波、音波的效果

    用到的工具 animation box-shadow html: <div class="watersource"> </div> css: .waters ...

  6. CF352A Jeff and Digits

    Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and ...

  7. Centos7安装MySQL8.0

    请到这个地址看:https://www.cnblogs.com/kevingrace/p/10482469.html Centos7安装MySQL8.0 - 操作手册 一.yum安装方式: 卸载之前版 ...

  8. [Leetcode]016. 3Sum Closest

    public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...

  9. 数据结构4.3_字符串模式匹配——KMP算法详解

    next数组表示字符串前后缀匹配的最大长度.是KMP算法的精髓所在.可以起到决定模式字符串右移多少长度以达到跳跃式匹配的高效模式. 以下是对next数组的解释: 如何求next数组: 相关链接:按顺序 ...

  10. 兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面

    // JavaScript Document// 加入收藏 < a onclick="AddFavorite(window.location,document.title)" ...