• springboot单元测试之MockMvc(二)


    1直接上代码,然后通过鼠标右键 run  as    Junit Test方法

    package com.example.demo;

    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.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.MvcResult;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    /**
    *
    * @author Administrator
    * mockMvc.perform是模拟客户端往服务端发送请求

    *andExpect 添加ResultMatcher-->MockMvcResultMatchers 验证规则
    * andReturn 最后返回相应的MvcResult

    */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes= {DemoApplication.class})
    @AutoConfigureMockMvc
    public class MockMvcTestDemo {

    @Autowired
    private MockMvc mockMvc;

    public void apiTest() throws Exception{
    MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/test/home"))
    .andExpect(MockMvcResultMatchers.status().isOk()).andReturn()
    ;
    int status=mvcResult.getResponse().getStatus();
    System.out.println("输出状态码:"+status);
    }

    }

  • 相关阅读:
    C语言I博客作业04
    C语言I博客作业02
    The First Assignment
    蒟蒻的长链剖分学习笔记(例题:HOTEL加强版、重建计划)
    分治FFT模板
    [Ynoi2016]掉进兔子洞 题解
    蒟蒻首开博客园博客QwQ
    bzoj4320 homework 题解
    [ZJOI2016]小星星(容斥+dp)
    【scoi2009】围豆豆(最短路模型)
  • 原文地址:https://www.cnblogs.com/zhushilai/p/13535180.html
Copyright © 2020-2023  润新知