MockHttpServletRequest.getAttribute()空指针

controller类@RequestMapping("/emps") public String getAll(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){ PageHelper.startPage(pn,10); List<Employee> emps = employeeService.getAll(); PageInfo pageInfo = new PageInfo(emps,6); model.addAttribute("pageInfo",pageInfo); return "sdfsd"; }

Test类@Test public void testPage() throws Exception { MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps"). param("pn", "3")).andReturn(); MockHttpServletRequest request = result.getRequest(); PageInfo pi = (PageInfo)request.getAttribute("pageInfo"); System.out.println("当前是第"+pi.getPageNum()); System.out.println("当前是第"+pi.getTotal()); List<Employee> list = pi.getList(); for (Employee e : list) { System.out.println(e.getEmpName()+" "+e.getEmail()); }

由于在controller类上面使用了@RestController注解
导致getAll()方法返回值为String的时候Test类中request.getAttribute(“pageInfo”);报空指针异常
而把返回值改为Model则可以获取数据

但将controller类上面的注解换成@Controller时
request.getAttribute(“pageInfo”);则可以正常获取数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值