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

