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

創(chuàng)新互聯(lián)專注于攀枝花企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城系統(tǒng)網(wǎng)站開發(fā)。攀枝花網(wǎng)站建設(shè)公司,為攀枝花等地區(qū)提供建站服務(wù)。全流程定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
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。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前文章:spring-mvc/springboot使用MockMvc對(duì)controller進(jìn)行測(cè)試
文章鏈接:http://chinadenli.net/article30/gghjpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站制作、網(wǎng)站改版、用戶體驗(yàn)、App開發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)