springBoot單元測試-模擬MVC測試


1)模擬mvc測試,和基礎測試是一樣的, 都需要在pom文件中引入junit的支持。

  略

 2)編寫測試類 Application1TestMVC

  在類頭上除啦加入之前的@RunWith(SpringRunner.class)、@RunWith(SpringRunner.class) 之外還要加入新的注解

  @AutoConfigureMockMvc // 注入MockMvc
 (當然你實在不想加也行,有其他辦法 , 不過我不想說,麻煩)
package com.cx.springboot;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.alibaba.fastjson.JSON;
import com.cx.springboot.hello1.model.UserModel;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc // 注入MockMvc
public class Application1TestMVC {

    @Autowired
    private MockMvc mvc;

    /**
     * 
     * @throws Exception
     * @創建時間 2018年7月13日
     * @功能描述  通過鏈接傳值 , 接受string 返回值   
     */
    @Test
    public void testString() throws Exception {
        //准備請求url  不用帶ip、端口、項目名稱等 直接寫接口的映射地址就可以了
        String url = "/app/get2/zhangsan/1";
        
        /* 構建request 發送請求GET請求
         * MockMvcRequestBuilders 中有很多 請求方式。像get、post、put、delete等等
         */
        MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(url)
                .accept(MediaType.APPLICATION_JSON)) // 斷言返回結果是json
                .andReturn();// 得到返回結果
        
        MockHttpServletResponse response = mvcResult.getResponse();
        //拿到請求返回碼
        int status = response.getStatus();
        //拿到結果
        String contentAsString = response.getContentAsString();

        System.err.println(status);
        System.err.println(contentAsString);
    }

    
    
    /**
     * 
     * @throws Exception
     * @創建時間 2018年7月13日
     * @功能描述  傳遞header ,接受 返回值   
     */
    @Test
    public void headerTest() throws Exception {
        // uri
        String uri = "/app/get4";
        
        MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri)
                .header("token", "asd123")
                .header("name", "zhangsan11")
                .accept(MediaType.APPLICATION_JSON)) // 斷言返回結果是json
                .andReturn();// 得到返回結果
        
        MockHttpServletResponse response = mvcResult.getResponse();
        //拿到請求返回碼
        int status = response.getStatus();
        //拿到結果
        String contentAsString = response.getContentAsString();
        
        System.err.println(status);
        System.err.println(contentAsString);
    }
    /**
     * 
     * @throws Exception
     * @創建時間 2018年7月13日
     * @功能描述  傳遞post請求和 bean類型對象 ,接受 返回值   
     */
    @Test
    public void postTest() throws Exception {
        // uri
        String uri = "/app/get3";
        
        UserModel userModel = new UserModel("張三", 11, new Date(), "abc123");
        
        MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(JSON.toJSONString(userModel))
                .accept(MediaType.APPLICATION_JSON)) // 斷言返回結果是json
                .andReturn();// 得到返回結果
        
        MockHttpServletResponse response = mvcResult.getResponse();
        //拿到請求返回碼
        int status = response.getStatus();
        //拿到結果
        String contentAsString = response.getContentAsString();
        
        System.err.println(status);
        System.err.println(contentAsString);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM