基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest),文中通過示例代碼介紹的非常詳細(xì),對大家的學(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.當(dāng)某個請求需要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)文章
解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實例重復(fù)消費(fèi)問題
這篇文章主要介紹了解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實例重復(fù)消費(fèi)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Maven使用Nexus創(chuàng)建私服的實現(xiàn)
本文主要介紹了Maven使用Nexus創(chuàng)建私服的實現(xiàn),通過建立自己的私服,就可以降低中央倉庫負(fù)荷、節(jié)省外網(wǎng)帶寬、加速M(fèi)aven構(gòu)建、自己部署構(gòu)件等,從而高效地使用Maven,感興趣的可以了解一下2024-04-04