您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“怎么在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。