Spring单元测试分页请求

思路:访问index.jsp页面,则其需要发出查询员工的请求 UserController接受请求,查出员工数据 来到list.jsp进行展示。
首先,我们需要对Controller进行编写
在这里插入图片描述
详细代码:

package com.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.beans.USer;
import com.ssm.service.UserService;

/*
 * 处理crud请求
 */
@Controller
public class UserController {
    
    
	@Autowired
	UserService userservice;
	@RequestMapping("/list")   //处理的就是我们的list请求  即查询员工数据
	public String getUser(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
    
      
		//pn   pagenumber,接收参数并设置默认值
		
		//为方便,我们引入了pageHelper分页插件,在查询之前,我们需要传入相关参数
		PageHelper.startPage(pn, 5);
		//startPage后面紧跟的查询就是分页的
		List<USer> userlist=userservice.getAll();
		//将查完的数据用pageInfo进行包装,只需要将pageinfo交给页面就可以了,里面有详细的信息
		PageInfo page=new PageInfo(userlist,2);  //这里的5是连续显示5页
		model.addAttribute("pageinfo", page);		
		return "list";
	}
}

而我们在使用这个插件时,则需要添加maven的依赖

<!-- 引入pagehelper分页插件 -->
	<dependency>
    	<groupId>com.github.pagehelper</groupId>
    	<artifactId>pagehelper</artifactId>
    	<version>5.0.0</version>
	</dependency>

我们需要先进行测试获取数据,即我们需要写一个测试类,而不是直接写jsp页面,这会方便我们的项目编写。
测试类:

package com.ssm.test;

import java.util.List;

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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;




import com.github.pagehelper.PageInfo;
import com.ssm.beans.USer;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={
    
    "classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/springmvc.xml"})
public class MvcTest {
    
    
	// 传入Springmvc的ioc
	@Autowired
	WebApplicationContext context;
	// 虚拟mvc请求,获取到处理结果。
	MockMvc mockMvc;

	@Before
	public void initMokcMvc() {
    
    
		mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
	}

	@Test
	public void testPage() throws Exception {
    
    
		//模拟请求拿到返回值
		MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/list").param("pn", "1"))
				.andReturn();
		
		//请求成功以后,请求域中会有pageInfo;我们可以取出pageInfo进行验证
		MockHttpServletRequest request = result.getRequest();
		PageInfo pi = (PageInfo) request.getAttribute("pageinfo");
		System.out.println("当前页码:"+pi.getPageNum());
		System.out.println("总页码:"+pi.getPages());
		System.out.println("总记录数:"+pi.getTotal());
		System.out.println("在页面需要连续显示的页码");
		int[] nums = pi.getNavigatepageNums();
		for (int i : nums) {
    
    
			System.out.print(" "+i);
		}
		
		//获取员工数据
		List<USer> list = pi.getList();
		for (USer employee : list) {
    
    
			System.out.println("ID:"+employee.getUserId()+"==>Name:"+employee.getUserName()+"==>部门:"+employee.getDept().getDeptName());
		}
		
	}
}

在这里插入图片描述
运行结果
在这里插入图片描述
那么,我们便实现了这个获取数据
此外,需要注意,对于联合表查询,我们在引入其他的实体类属性时一定要对应
在这里插入图片描述
那么,到这里我们便实现了Spring 单元测试分页请求

猜你喜欢

转载自blog.csdn.net/pengxiang1998/article/details/114290217