编写测试单元

    @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. 扫描线-小Z的桌子

    大概题意:在一个01矩阵中找到一个周长最大的全0矩形. 这道题用的是扫描线,O(n^2),求最大面积的思路完全可以放在这里.下面说说思路. 首先,一个最大周长子矩形(最大周长全0矩形),左右两侧的列上 ...

  2. 「BZOJ1010」[HNOI2008] 玩具装箱toy(斜率优化)

    P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为 1⋯N1\cdots N1⋯N ...

  3. LAMP实战之构建博客网站

    1.首先检查LAMP环境 [root@cairui htdocs]# ps -ef | grep httpd php Mar03 ? :: /opt/apache2.2.34/bin/httpd -k ...

  4. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

    原文链接:https://blog.csdn.net/hq091117/article/details/79065199 https://blog.csdn.net/allen_tsang/artic ...

  5. Oracle中merge into语法

    merge into 语句就是insert和update的一个封装,简单来说就是: 有则更新,无则插入 下面说怎么使用 MERGE INTO table_Name  T1(匿名) using (另外一 ...

  6. 线段树 SP1716 GSS3 - Can you answer these queries III

    SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...

  7. luogu1891 疯狂lcm ??欧拉反演?

    link 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N). 多组询问,1≤T≤300000,1≤N≤1000000 \(\sum_{i=1}^nlcm(i,n)\) \( ...

  8. IIS反向代理

  9. springboot整合mybatis,redis,代码(四)

    一 说明 这是spring整合redis注解开发的系类: 二 正文 在注解开发时候,会有这几个注解需要注意: 具体含义: 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时 ...

  10. leecode第三题(无重复字符的最长子串)

    class Solution { public: int lengthOfLongestSubstring(string s) { int len=s.size(); ||len==)//边界 ret ...