基于MockMvc進行springboot調(diào)試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進行springboot調(diào)試(SpringbootTest),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
測試前關(guān)閉web項目。springboot啟動程序WebApplication.class
筆者本地自定了端口SpringBootTest.WebEnvironment.DEFINED_PORT
代碼如下:
import com.netmarch.web.WebApplication;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
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.time.Instant;
import java.util.Random;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
public class TestAppController {
@Autowired
private WebApplicationContext context;
@Autowired
private MockMvc mvc;
private MockHttpSession session;// 1.定義一個變量保存session
String pathOnClasspath;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
session = new MockHttpSession(); //2.初始化
}
@Test
public void login() throws Exception {
// 登陸
MockHttpServletRequestBuilder loginRequestBuilder = MockMvcRequestBuilders.post("/user2/login")
.param("loginName", "test")
.param("password", "567")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON)
.session(session);//3.當某個請求需要session時,直接在構(gòu)造器中綁定需要的session
mvc.perform(loginRequestBuilder).andDo(MockMvcResultHandlers.print());
}
@Test
public void save() throws Exception {
//先登錄
login();
mvc.perform(post("/app/save")
.param("name","測試")
.param("categoryId","567")
.param("description","休閑益智類游戲語音識別測試")
.session(session)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
//.andExpect(jsonPath("$",hasSize(1)))
//.andExpect(jsonPath("$.message").value(is("保存成功")))
//.andExpect(jsonPath("$.message"),is("保存成功"))
.andDo(MockMvcResultHandlers.print());
}
@Test
public void update() throws Exception{
Random rnd = new Random();
int id = rnd.nextInt(6);
mvc.perform(
post("/app/update")
.param("id", String.valueOf(id))
.param("name", String.format("測試%s", Instant.now().toEpochMilli()))
.param("description", "描述12121")
).andDo(MockMvcResultHandlers.print());
}
@Test
public void list() throws Exception {
mvc.perform(get("/app/list")
.contentType(MediaType.TEXT_HTML))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
@Test
public void filteredList() throws Exception {
mvc.perform(post("/app/list")
.param("keyword","111")
.contentType(MediaType.TEXT_HTML))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
@Test
public void testisDuplicatedName() throws Exception
{
mvc.perform(post("/app/isDuplicatedName")
.param("name","測試")
).andDo(MockMvcResultHandlers.print());
}
}
測試輸出效果

其他參考:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot2.x+Quartz分布式集群的實現(xiàn)
這篇文章主要介紹了Springboot2.x+Quartz分布式集群的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java內(nèi)存緩存工具Guava LoadingCache使用解析
這篇文章主要介紹了Java內(nèi)存緩存工具Guava LoadingCache使用解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Java實現(xiàn)File轉(zhuǎn)換MultipartFile格式的例子
本文主要介紹了Java實現(xiàn)File轉(zhuǎn)換MultipartFile格式的例子,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
將JSON字符串數(shù)組轉(zhuǎn)對象集合方法步驟
這篇文章主要給大家介紹了關(guān)于將JSON字符串數(shù)組轉(zhuǎn)對象集合的方法步驟,文中通過代碼示例介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
IntelliJ IDEA maven 構(gòu)建簡單springmvc項目(圖文教程)
在工作當中,我們有時需要創(chuàng)建一個全新的工程,而基于spring-mvc web的工程較為常見,這篇文章主要介紹了IntelliJ IDEA maven 構(gòu)建簡單springmvc項目(圖文教程),感興趣的小伙伴們可以參考一下2018-05-05

