Get the App
SLTechnology News&Howtos  ›  Development  › 

How to debug springboot under MockMvc

Shulou Source: shulou.com Published: 2022-06-03 19:01:13 10月05日 Update

This article introduces the relevant knowledge of "how to debug springboot under MockMvc". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The code is as follows:

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) @ AutoConfigureMockMvcpublic class TestAppController {@ Autowired private WebApplicationContext context; @ Autowired private MockMvc mvc; private MockHttpSession session;// 1. Define a variable to hold session String pathOnClasspath; @ Before public void setUp () throws Exception {mvc = MockMvcBuilders.webAppContextSetup (context). Build (); session = new MockHttpSession (); / / 2. Initialize} @ Test public void login () throws Exception {/ / Log in MockHttpServletRequestBuilder loginRequestBuilder = MockMvcRequestBuilders.post ("/ user2/login") .param ("loginName", "test") .param ("password", "567") .contentType (MediaType.APPLICATION_JSON_UTF8) .accept (MediaType.APPLICATION_JSON) .session (session); / / 3. When a request requires session, bind the required session mvc.perform (loginRequestBuilder) .andDo (MockMvcResultHandlers.print ()) directly in the constructor;} @ Test public void save () throws Exception {/ / log in to login () first Mvc.perform (post ("/ app/save") .param ("name", "test") .param ("categoryId", "567") .param ("description", "speech recognition test of leisure educational games") .session (session) .contentType (MediaType.APPLICATION_JSON) .andexpect (status (). IsOk ()) / .andExpect (jsonPath ("$") HasSize (1)) / / .andExpect (jsonPath ("$.message") .value (is ("saved successfully")) / / .andExpect (jsonPath ("$.message"), is ("saved successfully") .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 ("Test% s", Instant.now (). ToEpochMilli ()) .param ("description", "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", "test") .andDo (MockMvcResultHandlers.print ());}}

Test output effect

This is the end of "how to debug springboot under MockMvc". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Testing success content more knowledge output practicality learning next code variables dilemmas reality situation effects articles cases educational classes editing websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information vpn Xiaomi Shulou Technology