美文网首页
MockMVC使用总结

MockMVC使用总结

作者: 少寨主的互联网洞察 | 来源:发表于2018-09-05 14:39 被阅读0次

    基础

    • controller:
    @Controller
    public class EmploeeController { 
        @Autowired
        EmploeeService emploeeService;
        /**
         * @return
         * 查询员工数据(分页查询)
         */
        @RequestMapping("/emps")
        public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,
                Model model) {
            PageHelper.startPage(pn, 5);
            List<Emploee> emps=emploeeService.getAll();
            PageInfo page=new PageInfo(emps,5);
            model.addAttribute("pageInfo",page);
            return "list";
        }
    }
    
    • service:
    @Service
    public class EmploeeService {
        @Autowired
        EmploeeMapper emploeeMapper;
        public List<Emploee> getAll() {
            // TODO Auto-generated method stub
            return emploeeMapper.selectByExampleWithDept(null);
        }
    
    }
    
    • dao:
    List<Emploee> selectByExampleWithDept(EmploeeExample example);
    
    • mapper:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.cqu.dao.EmploeeMapper">
      <resultMap type="com.cqu.bean.Emploee" id="WithDeptResultMap">
        <id column="emp_id" jdbcType="INTEGER" property="empId" />
        <result column="emp_name" jdbcType="VARCHAR" property="empName" />
        <result column="gender" jdbcType="BIT" property="gender" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="d_id" jdbcType="INTEGER" property="dId" />
        <association property="department" javaType="com.cqu.bean.Department">
            <id column="dept_id" property="deptId"/>
            <result column="dept_name" property="deptName"/>
        </association>
      </resultMap>
    
    <sql id="WithDept_Colum_List">
        e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
      </sql>
    
    <select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
        select
        <if test="distinct">
          distinct
        </if>
        <include refid="WithDept_Colum_List" />
        FROM tbl_emp e
            left join tbl_dept d on e.`d_id`=d.`dept_id`
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
          order by ${orderByClause}
        </if>
      </select>
    </mapper>
    
    • 数据库表结构


      图片.png

    测试部分

    • MVCTest.java
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @ContextConfiguration(locations= {"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})
    public class MVCTest {
        @Autowired
        WebApplicationContext context;
        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();
            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<Emploee> list=pi.getList();
            for(Emploee emploee:list) {
                System.out.println("ID:"+emploee.getEmpId()+"==>Name:"+emploee.getEmpName());
            }
        }
    }
    
    • 返回展示数据
    当前页码:1
    总页码:21
    总记录数:102
    当前页面需要连续显示的页码:
     1 2 3 4 5ID:1==>Name:Tom
    ID:2==>Name:Tom
    ID:3==>Name:d4a8b0
    ID:4==>Name:590b31
    ID:5==>Name:ed92b2
    

    总结

    • 测试代码关键部分
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @ContextConfiguration(locations= {"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})
    public class MVCTest {
        @Autowired
        WebApplicationContext context;
        MockMvc mockMVC;
        @Before
        public void initMockMvc() {
            mockMVC=MockMvcBuilders.webAppContextSetup(context).build();
        }
    
    • 意义
      简化测试流程,不用直接生成网络请求环境,Mock方式模拟HTTP请求。

    相关文章

      网友评论

          本文标题:MockMVC使用总结

          本文链接:https://www.haomeiwen.com/subject/lpxawftx.html