CRUD(14):查询-单元测试分页请求

在com.amel.crud.test新建mvcTest类。


package com.amel.crud.test;







import java.util.List;







import javax.naming.spi.ResolveResult;







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 com.amel.crud.bean.Employee;



import com.github.pagehelper.PageInfo;







/**



 * 使用Spring测试模块提供的测试请求功能,测试CRUD请求的正确性



 * @author lenovo



 *



 */







@RunWith(SpringJUnit4ClassRunner.class)



@WebAppConfiguration



@ContextConfiguration(locations = { "classpath:applicationContext.xml" 

,"file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})



public class mvcTest {



       //传入springmvc的ioc



       @Autowired



       WebApplicationContext context;



       //虚拟mvc请求,获取处理结果



       MockMvc mockMvc;



       



       @Before



       public void initMockMvc() {



             mockMvc = MockMvcBuilders.webAppContextSetup(context).build();



       }



       



       @Test



       public void testPage() throws Exception {



             //模拟请求拿到返回值



             MvcResult result = 

mockMvc.perform(MockMvcRequestBuilders.get("/emps").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.println(" "+i);



             }



             



             //获取员工数据



             List<Employee> list = pi.getList();



             for(Employee employee : list) {



                    System.out.println("ID:"+employee.getEmpId()+"  姓名:"+employee.getEmpName());



             }



                    



       }







}



错误提示:

报错:javax/servlet/SessionCookieConfig

原因:请检查你的servlet api是否在3.0版本以上。

posted on 2021-02-16 00:01  northwest  阅读(55)  评论(0)    收藏  举报

导航