如何使用spring測試模組測試請求功能

weixin_34337265發表於2018-09-07

使用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());
}
    }

五、執行測試

然後就把測試資料拿出來了。

5391552-5a0f85f13cb84c36.png
image.png

需要注意的是對MockMVC的初始化以及WebApplicationContext的注入,其他的都是常規操作。

相關文章