spring-mvc/springboot使用MockMvc對(duì)controller進(jìn)行測(cè)試
網(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。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2019-09-09String.intern()作用與常量池關(guān)系示例解析
這篇文章主要為大家介紹了String.intern()作用與常量池關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Spring Boot利用Thymeleaf發(fā)送Email的方法教程
spring Boot默認(rèn)就是使用thymeleaf模板引擎的,下面這篇文章主要給大家介紹了關(guān)于在Spring Boot中利用Thymeleaf發(fā)送Email的方法教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-08-08Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法
短信驗(yàn)證功能在各個(gè)網(wǎng)站應(yīng)用都非常廣泛,那么在springmvc中如何實(shí)現(xiàn)短信驗(yàn)證碼功能呢?今天小編抽時(shí)間給大家介紹下Spring MVC 中 短信驗(yàn)證碼功能的實(shí)現(xiàn)方法,一起看看吧2016-09-09Google Kaptcha 框架實(shí)現(xiàn)登錄驗(yàn)證碼功能(SSM 和 SpringBoot)
這篇文章主要介紹了Google Kaptcha 實(shí)現(xiàn)登錄驗(yàn)證碼(SSM 和 SpringBoot)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊(duì)列詳解
這篇文章主要介紹了Java常見的數(shù)據(jù)結(jié)構(gòu)之棧和隊(duì)列詳解,棧(Stack) 是一種基本的數(shù)據(jù)結(jié)構(gòu),具有后進(jìn)先出(LIFO)的特性,類似于現(xiàn)實(shí)生活中的一疊盤子,棧用于存儲(chǔ)一組元素,但只允許在棧頂進(jìn)行插入(入棧)和刪除(出棧)操作,需要的朋友可以參考下2023-10-10Spring?Boot?實(shí)現(xiàn)?WebSocket?的代碼示例
WebSocket?協(xié)議是獨(dú)立的基于?TCP?協(xié)議。它與?HTTP?的唯一關(guān)系是,它的握手會(huì)被?HTTP?服務(wù)器解釋為?Upgrade?請(qǐng)求,接下來通過本文給大家介紹Spring?Boot?實(shí)現(xiàn)?WebSocket?示例詳解,需要的朋友可以參考下2022-04-04java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07