欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC单元测试

程序员文章站 2022-04-29 16:45:49
...

分页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.atguigu.bean.Employee;
import com.atguigu.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService;
	
	@RequestMapping("/emps")
	public String getEmps(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model) {
		
//		System.out.println("employeeService==" + employeeService);
//		System.out.println("pn===" + pn );
		//引入PageHelper插件
		PageHelper.startPage(pn, 5);
		//startPage后面紧跟的这个查询就是一个分页查询
		List<Employee> emps = employeeService.getAll();
//		System.out.println("emps===" + emps);
		//使用pageInfo包装查询后的结果,只需要将pageInfo交给页面就可以了
		//封装了详细的分页信息,包括有我们查询出来的数据,传入连续显示的页数
		PageInfo<Employee> pageInfo = new PageInfo<Employee>(emps,5);
		model.addAttribute("pageInfo", pageInfo);
		return "list";
	}
}


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.test.context.ContextConfiguration;
import org.springframework.mock.web.MockHttpServletRequest;
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.atguigu.bean.Employee;
import com.github.pagehelper.PageInfo;

/**
 * 使用Spring测试模块提供的测试来测试请求功能
 * 测试crud的正确性
 * @author MaJiatao
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration ({"classpath*:applicationContext.xml","classpath*:springmvc.xml"})
public class MvcTest {
	//传入SpringMVC的ioc
	@Autowired
	WebApplicationContext context;
	//虚拟mvc请求,获取到处理结果
	MockMvc mockMvc;
	
	@Before
	public void initMockMvc() {
		mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
//		System.out.println("mockMvc====" + mockMvc);
	}
	
	@Test
	public void testPage() throws Exception {
		MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
		//请求成功后,请求域中会有pageInfo,我们可以取出pageInfo进行验证
//		System.out.println("result===" + result);
		MockHttpServletRequest request = result.getRequest();
		PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
		System.out.println("当前页码:" + pageInfo.getPageNum());
		System.out.println("总页码数:" + pageInfo.getPages());
		System.out.println("总记录数:" + pageInfo.getTotal());
		System.out.print("在页面需要连续显示的页码:" );
		int[] nums = pageInfo.getNavigatepageNums();
		for (int i : nums) {
			System.out.print(i + " ");
		}
		System.out.println();
		System.out.println("获取的员工数据:");
		List<Employee> list = pageInfo.getList();
		for (Employee employee : list) {
			System.out.println(employee);
		}
	}
}