基于MockMvc进行springboot调试(SpringbootTest)

 更新时间:2019年10月15日 10:41:38   作者:你不知道的浪漫  
这篇文章主要介绍了基于MockMvc进行springboot调试(SpringbootTest),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了基于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());
  }


}

测试输出效果

其他参考:

https://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-write-clean-assertions-with-jsonpath/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java文件(io)编程_基于File类的基本用法(必看篇)

    Java文件(io)编程_基于File类的基本用法(必看篇)

    下面小编就为大家带来一篇Java文件(io)编程_基于File类的基本用法(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • mybatis-plus分页查询三种方法小结

    mybatis-plus分页查询三种方法小结

    本文主要介绍了mybatis-plus分页查询三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Springboot引入多个yml方法(多种方案)

    Springboot引入多个yml方法(多种方案)

    SpringBoot默认加载的是application.yml文件,所以想要引入其他配置的yml文件,就要在application.yml中激活该文件这篇文章主要介绍了Springboot引入多个yml方法,需要的朋友可以参考下
    2019-10-10
  • Java实现五子棋游戏的完整代码

    Java实现五子棋游戏的完整代码

    这篇文章主要为大家详细介绍了Java实现五子棋游戏的完整代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • springboot集成nacos报错:get data from Nacos error,dataId:null.yaml的原因及解决方法

    springboot集成nacos报错:get data from Nacos 

    这篇文章给大家介绍了springboot集成nacos报错:get data from Nacos error,dataId:null.yaml的原因及解决方法,如果又遇到相同问题的朋友可以参考阅读本文
    2023-10-10
  • 一分钟掌握Java ElasticJob分布式定时任务

    一分钟掌握Java ElasticJob分布式定时任务

    ElasticJob 是面向互联网生态和海量任务的分布式调度解决方案,本文主要通过简单的示例带大家深入了解ElasticJob分布式定时任务的相关知识,需要的可以参考一下
    2023-05-05
  • Java设计模式之单例模式简介

    Java设计模式之单例模式简介

    这篇文章主要介绍了Java设计模式之单例模式简介,文中有非常详细的代码示例,对正在学习Java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • java实现上传图片尺寸修改和质量压缩

    java实现上传图片尺寸修改和质量压缩

    这篇文章主要为大家详细介绍了java实现上传图片尺寸修改和质量压缩,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 有关tomcat内存溢出的完美解决方法

    有关tomcat内存溢出的完美解决方法

    下面小编就为大家带来一篇有关tomcat内存溢出的完美解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • Jmeter压力测试简单教程(包括服务器状态监控)

    Jmeter压力测试简单教程(包括服务器状态监控)

    Jmeter是一个非常好用的压力测试工具。Jmeter用来做轻量级的压力测试,非常合适,本文详细的介绍了Jmeter的使用,感性的可以了解一下
    2021-11-11

最新评论