Springboot整合MockMvc 測試


傳參問題

GET[select]

1.路徑參數 : @PathVariable

2.表單參數 : @RequestParam

 

POST[insert]: 1.JSON請求體參數  :  @RequestBody

 

PUT[update]: 1.路徑參數  :  @PathVariable

2.表單參數  :  @RequestParam

 

DELETE[delete]: 1.路徑參數  :  @PathVariable

2.表單參數  :  @RequestParam

 

MockMvc:

1.路徑請求

mockMvc.perform(MockMvcRequestBuilders
.請求方式(“url
/ {path}”,參數值)

示例:
//執行請求(使用GET請求)
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders
.get("/vnone/{userId}","123") //路徑傳參.
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print()) // MockMvcResultHandlers.print() 日志打印會更清晰
.andReturn();


2.表單請求 mockMvc.perform(MockMvcRequestBuilders .請求方式(“url”).param(“鍵”,“值”).contentType(MediaType.APPLICATION_FORM_URLENCODED)

示例:
 
         
//執行請求(使用GET請求)
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders
.get("/vnone")
.param("mailNo", "SF12345678") //路徑傳參.
.accept(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print()) // MockMvcResultHandlers.print() 日志打印會更清晰
.andReturn();


3.JSON請求

示例:

String requestBody = "{\"id\":1, \"name\":\"zhang\"}"; MvcResult mvcResult = mockMvc.perform(post("/user")  .contentType(MediaType.APPLICATION_JSON).content(requestBody)  .accept(MediaType.APPLICATION_JSON)) //執行請求   .andExpect(jsonPath("$.id").value(1)); //使用Json path驗證JSON 請參考http://goessner.net/articles/JsonPath/  
異步測試:

//Callable  
    MvcResult result = mockMvc.perform(get("/user/async1?id=1&name=zhang")) //執行請求  
            .andExpect(request().asyncStarted())  
            .andExpect(request().asyncResult(CoreMatchers.instanceOf(User.class))) //默認會等10秒超時  
            .andReturn();  
      
    mockMvc.perform(asyncDispatch(result))  
            .andExpect(status().isOk())  
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))  
            .andExpect(jsonPath("$.id").value(1));  
文件上傳:

byte[] bytes = new byte[] {1, 2};  
mockMvc.perform(fileUpload("/user/{id}/icon", 1L).file("icon", bytes)) //執行文件上傳  
        .andExpect(model().attribute("icon", bytes)) //驗證屬性相等性  
        .andExpect(view().name("success")); //驗證視圖 
驗證請求參數綁定到模型數據及Flash屬性:

mockMvc.perform(post("/user").param("name", "zhang")) //執行傳遞參數的POST請求(也可以post("/user?name=zhang"))  
            .andExpect(handler().handlerType(UserController.class)) //驗證執行的控制器類型  
            .andExpect(handler().methodName("create")) //驗證執行的控制器方法名  
            .andExpect(model().hasNoErrors()) //驗證頁面沒有錯誤  
            .andExpect(flash().attributeExists("success")) //驗證存在flash屬性  
            .andExpect(view().name("redirect:/user")); //驗證視圖  

 

測試類:

 

package com.vn.vnone;

import com.vn.vnone.service.VnoneService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
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.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.in;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {VnoneApplication.class, MockServletContext.class})
@WebAppConfiguration
@Slf4j
class VnoneApplicationTests {

    /**
     * 引入 webApplicationContext 之后,才可以在測試類中使用  servcie 、dao層注入的對象
     */
    @Autowired
    private WebApplicationContext webApplicationContext;

    @Mock
    private VnoneService vnoneService;

    private MockMvc mvc;

    // Spring Boot 2.2.9 默認使用的是junit 5,在junit 5中 @Before 被 @BeforeEach代替。
    @BeforeEach
    public void setUp() throws Exception {

        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    void getUser() throws Exception {

        // 測試VnoneController
        RequestBuilder request = null;

        // 1、get查一下user列表,應該為空
        request = get("/vnone");
        mvc.perform(request)
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("vn/vnone")))
                .andDo(MockMvcResultHandlers.print());

    }

    @Test
    void createUser() throws Exception {


        //執行請求(使用POST請求)
        MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders
                        .post("/vnone")
                        .param("username", "vn")
                        .param("age", "10")  //若無參數則不寫param.
                        .accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(MockMvcResultHandlers.print())  // MockMvcResultHandlers.print()  日志打印會更清晰
                .andReturn();

        //獲取返回編碼
        int status = mvcResult.getResponse().getStatus();
        //獲取返回結果
        String content = mvcResult.getResponse().getContentAsString();
        //斷言,判斷返回編碼是否正確
        Assert.assertEquals(200, status);
        System.out.println("--------返回的json = " + content);

    }

}

 


免責聲明!

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



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