基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
測(cè)試前關(guān)閉web項(xiàng)目。springboot啟動(dòng)程序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.定義一個(gè)變量保存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)某個(gè)請(qǐng)求需要session時(shí),直接在構(gòu)造器中綁定需要的session mvc.perform(loginRequestBuilder).andDo(MockMvcResultHandlers.print()); } @Test public void save() throws Exception { //先登錄 login(); mvc.perform(post("/app/save") .param("name","測(cè)試") .param("categoryId","567") .param("description","休閑益智類游戲語(yǔ)音識(shí)別測(cè)試") .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("測(cè)試%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","測(cè)試") ).andDo(MockMvcResultHandlers.print()); } }
測(cè)試輸出效果
其他參考:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問(wèn)題
這篇文章主要介紹了解決rocketmq-spring-boot-starter導(dǎo)致的多消費(fèi)者實(shí)例重復(fù)消費(fèi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06java多線程解決生產(chǎn)者消費(fèi)者問(wèn)題
這篇文章主要介紹了java多線程解決生產(chǎn)者消費(fèi)者問(wèn)題的方法,實(shí)例分析了java采用多線程的方法解決生產(chǎn)者消費(fèi)者問(wèn)題的相關(guān)技巧,需要的朋友可以參考下2015-05-05Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn)
這篇文章主要介紹了Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Maven使用Nexus創(chuàng)建私服的實(shí)現(xiàn)
本文主要介紹了Maven使用Nexus創(chuàng)建私服的實(shí)現(xiàn),通過(guò)建立自己的私服,就可以降低中央倉(cāng)庫(kù)負(fù)荷、節(jié)省外網(wǎng)帶寬、加速M(fèi)aven構(gòu)建、自己部署構(gòu)件等,從而高效地使用Maven,感興趣的可以了解一下2024-04-04