package com.wzy.test;import com.github.pagehelper.PageInfo;import com.wzy.pojo.Employee;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mock.web.MockHttpServletRequest;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MockMvcBuilder;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import java.util.List;@WebAppConfiguration@RunWith(SpringJUnit4ClassRunner.class)//指定使用SpringJUnit4单元测试运行@ContextConfiguration(locations = {"classpath:springapplication.xml","classpath:dispatcherServlet-Servlet.xml"})public class MVCTest {//传入springMVC的ioc@AutowiredWebApplicationContext context;//虚拟的MVCMockMvc mockMvc;@Beforepublic void initMockMVC(){mockMvc = MockMvcBuilders.webAppContextSetup(context).build();}@Testpublic void testPage() throws Exception {/*** MockMvcRequestBuilders.get("/list"):发起get请求* .param("pageNumber","1"):在/list后面携带 参数* .andReturn():获取返回值*/MvcResult pageNumber = mockMvc.perform(MockMvcRequestBuilders.get("/list").param("pageNumber", "10")).andReturn();//发起get请求//请求成功后,请求域中会有保存的pageInfo,可以取出 pageInfoMockHttpServletRequest request = pageNumber.getRequest();PageInfo pageInfo = (PageInfo)request.getAttribute("pageInfo");//取出当前页码System.out.println("当前页码:" + pageInfo.getPageNum());System.out.println("总页码:" + pageInfo.getPages());System.out.println("总记录数:" + pageInfo.getTotal());System.out.println("========在页面需要连续显示的页码============");int[] navigatepageNums = pageInfo.getNavigatepageNums();for (int page : navigatepageNums) {System.out.print(page + "\0");}System.out.println("\n当前页数据!");//获取员工数据List<Employee> list = pageInfo.getList();for (Employee employee : list) {System.out.println("ID:" + employee.getEmpId() +";\0姓名:" + employee.getEmpName() +";\0性别:" + employee.getGender() +";\0邮箱:" + employee.getEmail());}}}
结果:
当前页码:10 总页码:21 总记录数:101
========在页面需要连续显示的页码============
6 7 8 9 10 11 12 13 当前页数据!
ID:86; 姓名:71b7184; 性别:男; 邮箱:d321384@163.com
ID:92; 姓名:d9e1b90; 性别:男; 邮箱:b1bb990@163.com
ID:95; 姓名:ab00293; 性别:女; 邮箱:74c2c93@163.com
ID:96; 姓名:4273394; 性别:男; 邮箱:153a294@163.com
ID:98; 姓名:bb90a96; 性别:男; 邮箱:a520b96@163.com
