您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么在MockMvc下進行springboot調試”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
代碼如下:
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時,直接在構造器中綁定需要的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()); } }
測試輸出效果
“怎么在MockMvc下進行springboot調試”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。