如何使用spring测试模块测试请求功能

2018-09-07  本文已影响0人  无能的计科狗

使用spring测试模块测试请求功能

SSM项目中使用了PageHelper插件对数据进行拦截与分页。

一、利用Spring提供的测试模块模拟请求

首先引入相关依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qroxy</groupId>
  <artifactId>ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <!-- 引入项目依赖包 -->
  <!-- springMVC -->
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>
  <!--spring-Jdbc  -->
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>
    <!-- spring 面向切面编程 -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.7.RELEASE</version>
    <!--mybatis-->
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->

</dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.2</version>
</dependency>
    <!-- mybtis整合适配包 -->
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>
    
<!-- springTest(本次重点) -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.7.RELEASE</version>
    <scope>test</scope>
</dependency>
<!--引入PageHelper分页插件(本次重点)  -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.0.0</version>
</dependency>
    <!--  数据库连接池-->
    <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
    <!-- mysql 驱动包 -->
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>
<!-- web工程标配包(jstl,servlet-api,junit) -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <!-- scop标签作用:服务器已经有 -->
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- 逆向工程包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>

  </dependencies>
</project>
二、在 MyBatis 配置 xml 中配置拦截器插件
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>

ps:必须在<typeAliases>后面配置插件

三、新建一个测试类

1、引入spring的单元测试使用注解和加载配置文件的注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(配置文件路径)

接着必须再加一个@WebAppConfiguration注解,因为初始化MockMvc时需要注入SpringMVC本身。

使用MockMVC作为虚拟的MVC
//  虚拟的mvc请求,获取处理结果
    @Autowired
    WebApplicationContext wContext;
    MockMvc mockMvc;
//  每次用前初始化,ps:用的是junit的before
    @Before
    public void initMockMvc() {
        mockMvc=MockMvcBuilders.webAppContextSetup(wContext).build();
    }
四、利用虚拟MVC模拟页面请求,请求员工数据并做分页
@Test 
    public void testPAge() throws Exception {
        MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/emps")
                .param("pn", "5")).andReturn();
//      请求成功后,请求域中会有pageINfo,我们取出pageInfo进行验证
        MockHttpServletRequest request=mvcResult.getRequest();
    <!--  PageInfo包装数据强行转换-->
    PageInfo aInfo=(PageInfo)request.getAttribute("PageInfo");
    System.out.println("当前页码:"+aInfo.getPageNum());
    System.out.println("总页码:"+aInfo.getPages());
    System.out.println("总记录数:"+aInfo.getTotal());
    System.out.println("在页面需要连续显示的的页码:");
    int[] nums=aInfo.getNavigatepageNums();
    for(int i:nums) {
        System.out.println(" "+i);
    }
//获取员工数据
List<Employee> list=aInfo.getList();
for(Employee employee:list) {
    System.out.println("ID "+employee.getEmpId()+",name:"+employee.getEmpName());
}
    }

五、运行测试

然后就把测试数据拿出来了。

image.png
需要注意的是对MockMVC的初始化以及WebApplicationContext的注入,其他的都是常规操作。
上一篇下一篇

猜你喜欢

热点阅读