编写测试单元

    @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. L - Large Division (大数, 同余)

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  2. 对接极光IM之修改头像

    因为项目中使用了极光IM,在对接极光的时候,发现了如果想要在改变自己个人中心的头像同时改变极光IM的头像,就必须要将本地磁盘的文件上传到极光服务器,根据反馈的media_id来进行修改头像. 但是因为 ...

  3. Nginx+ISS+Redis实现完美负载均衡

    前篇文章讲到nginx是使网站采用分布式,对用户的请求采用分布式,分配到不同的服务器上,然后进行同一站点的访问,保证了访问的高效,使用率高,生命期长. 说到ISS,这里重点介绍tomcat,Tomca ...

  4. poj1830(高斯消元解mod2方程组)

    题目链接:http://poj.org/problem?id=1830 题意:中文题诶- 思路:高斯消元解 mod2 方程组 有 n 个变元,根据给出的条件列 n 个方程组,初始状态和终止状态不同的位 ...

  5. Python第一次写的代码

    #!/bin/bash/env python # -*- coding:utf-8 -*- #function:输出1-10每隔1秒 import time start = 1 flag = True ...

  6. cuda&vs2010的属性配置

    平时总需要新建工程,但是却总忘记该修改哪里,于是寻找了官方的项目,截下其中的属性修改图. 1 2 3 4 5 6 7 8 9 10 11

  7. Error: connection reset by peer ,during filebeat connect to elk.

    Error screenshot like below: Reason: What I found that was the machine failing had same configuratio ...

  8. export to pdf

    first we need to download the link is : http://files.cnblogs.com/akingyao/itextsharp-all-5.4.2.zip t ...

  9. rem原理

    rem布局实际上就是实现等比缩放 试想,如果我们的元素在不同的屏幕上可以按照相同的比例来进行缩放就好了. rem的计算原理: 试想把屏幕平均分成10份,那么每一份就是1/10,我们选择每一份的大小是1 ...

  10. C++并发低级接口:std::thread和std::promise

    std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...