Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to debug springboot under MockMvc

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report