基于MockMvc进行springboot调试(SpringbootTest)
2019-10-16 18:07
781 查看
这篇文章主要介绍了基于MockMvc进行springboot调试(SpringbootTest),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
测试前关闭web项目。springboot启动程序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.定义一个变量保存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()); } }
测试输出效果
其他参考:
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 基于Springboot使用MongoDB进行简单Gis操作
- springboot(二十):使用spring-boot-admin对spring-boot服务进行监控
- 【转】springboot+websocket+sockjs进行消息推送【基于STOMP协议】
- Spring boot 中的事务管理,并基于junit进行测试
- Intellij IDEA基于Springboot的远程调试
- 【死磕springboot2.0】springboot基于web开发
- SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送
- 【死磕springboot2.0】springboot基于web开发入门
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式)
- idea中对springboot进行远程调试
- spring-mvc/springboot使用MockMvc对controller进行测试
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
- 基于spring boot对java 轻博客系统tale进行重构学习
- gradle启动springboot项目进行远程调试
- 基于spring test框架进行单元测试-框架介绍
- SpringBoot工程+热部署进行远程调试
- spring-mvc springboot 使用MockMvc对controller进行测试
- SpringBoot:使用spring-boot-devtools进行热部署不生效的问题解决方法
- SpringBoot集成WebSocket【基于STOMP协议】进行点对点[一对一]和广播[一对多]实时推送
- Intellij IDEA基于Springboot的远程调试(图文)