溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring-mvc/springboot使用MockMvc對(duì)controller進(jìn)行測(cè)試

發(fā)布時(shí)間:2020-10-19 06:00:07 來源:腳本之家 閱讀:178 作者:火光閃耀 欄目:編程語言

網(wǎng)上基本都是參考官方的使用方式,使用了import static,個(gè)人感覺這種方式特別不好,代碼提示性不友好。所以在此進(jìn)行說明,也方便自己以后使用。

1. 引入spring-test相關(guān)jar包,springboot只需引入spring-boot-starter-test即可

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

2. 寫好controller,開始寫test類

import org.front.server.Application;
import org.front.server.web.control.TestController;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
//網(wǎng)上很多會(huì)在這里使用import static,主要導(dǎo)入的是MockMvcRequestBuilders,MockMvcResultMatchers,Matchers這三個(gè)類中的方法。
/**
 * @author zz
 * @date 2017年7月4日
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes = MockServletContext.class)//這個(gè)測(cè)試單個(gè)controller,不建議使用
@SpringApplicationConfiguration(classes = Application.class)//這里的Application是springboot的啟動(dòng)類名。
@WebAppConfiguration
public class ApplicationTests {
  @Autowired
  private WebApplicationContext context;
  private MockMvc mvc;
  
  @Before
  public void setUp() throws Exception {
 //    mvc = MockMvcBuilders.standaloneSetup(new TestController()).build();
    mvc = MockMvcBuilders.webAppContextSetup(context).build();//建議使用這種
  }
  @Test
  public void test1() throws Exception {
    mvc.perform(MockMvcRequestBuilders.get("/data/getMarkers")
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .param("lat", "123.123").param("lon", "456.456")
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andDo(MockMvcResultHandlers.print())
        .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("SUCCESS")));
    
  }
}

相信這樣,基本開發(fā)過javaweb的就都能看懂了。通過方法的字面意思應(yīng)該都能看懂方法含義,如果實(shí)在不懂請(qǐng)看源碼或者官方API。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI