pom依赖
确认已经添加依赖
- junit
- spring-test
测试类添加注解
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {"classpath:conf/applicationContext.xml", "classpath:conf/dispatcherServlet.xml"})
自动注入web容器
@Autowired private WebApplicationContext context;
初始化MocMvc
@Before public void initMockMvc() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); }
发送请求并获得一个返回
MvcResult aReturn = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pageNum", "123")).andReturn();
EmployeeController.java
package top.djosimon.controller; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.apache.ibatis.annotations.Param; 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 top.djosimon.domain.Employee; import top.djosimon.service.EmployeeService; import java.util.List; /** * @author dj * @date 2020/12/10 */ @Controller public class EmployeeController { @Autowired private EmployeeService service; @RequestMapping("/emps") public String findAllEmployee(@Param("pageNum") Integer pageNum, Model model) { // 当前页码为:pageNum,每一页显示记录条数为:5条 PageHelper.startPage(pageNum, 5); List<Employee> list = service.findAllEmployee(); // 导航条上的页码数为10个 PageInfo<Employee> info = PageInfo.of(list, 10); model.addAttribute("info", info); return "list"; } }
TestController.java
package top.djosimon; import com.github.pagehelper.PageInfo; 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.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; /** * @author dj * @date 2020/12/10 */ // 让测试运行于Spring测试环境 @RunWith(SpringJUnit4ClassRunner.class) // 表示使用服务器webapp的配置 @WebAppConfiguration // 指定spring、springmvc配置文件的位置 @ContextConfiguration(locations = {"classpath:conf/applicationContext.xml", "classpath:conf/dispatcherServlet.xml"}) public class TestController { // 自动注入web容器,需要用到注解 @WebAppConfiguration @Autowired private WebApplicationContext context; MockMvc mockMvc; // 初始化MockMvc @Before public void initMockMvc() { // 在webapp容器建立阶段开始构建MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testPages() throws Exception { MvcResult aReturn = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pageNum", "123")).andReturn(); // 构建一个请求生成器(生成器需要信息:请求uri、请求参数) MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/emps").param("pageNum", "123"); // 执行请求生成器并且得到Controller发送回来的result MvcResult result = mockMvc.perform(requestBuilder).andReturn(); // result获取请求作用域对象,再取得其中属性、信息。 MockHttpServletRequest request = result.getRequest(); PageInfo info = (PageInfo) request.getAttribute("info"); System.out.println("当前页码:" + info.getPageNum()); System.out.println("总页码:" + info.getPages()); System.out.println("总记录数:" + info.getTotal()); int[] nums = info.getNavigatepageNums(); System.out.print("当前导航页码:"); for (int num : nums) { System.out.print(" " + num); } } }
0 条评论