zoukankan      html  css  js  c++  java
  • SpringBootTest单元测试实战、SpringBoot测试进阶高级篇之MockMvc讲解

    1、@SpringBootTest单元测试实战

    简介:讲解SpringBoot的单元测试

    1、引入相关依赖

    <!--springboot程序测试依赖,如果是自动创建项目默认添加-->

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-test</artifactId>

                <scope>test</scope>

            </dependency>

    2、使用

             测试类:

    @RunWith(SpringRunner.class)  //底层用junit  SpringJUnit4ClassRunner

    @SpringBootTest(classes={XdclassApplication.class})//启动整个springboot工程

    public class SpringBootTests { }

          

    2、SpringBoot测试进阶高级篇之MockMvc讲解

    简介:讲解MockMvc类的使用和模拟Http请求实战

    1、增加类注解 @AutoConfigureMockMvc

    @SpringBootTest(classes={XdclassApplication.class})

    2、相关API

    perform:执行一个RequestBuilder请求

    andExpect:添加ResultMatcher->MockMvcResultMatchers验证规则

    andReturn:最后返回相应的MvcResult->Response

        代码示例:

        SampleControler.java:

     1 package net.xdclass.demo.controller;
     2 
     3 import java.util.Date;
     4 import java.util.HashMap;
     5 import java.util.Map;
     6 
     7 import org.springframework.boot.*;
     8 import org.springframework.boot.autoconfigure.*;
     9 import org.springframework.web.bind.annotation.*;
    10 
    11 import net.xdclass.demo.domain.User;
    12 
    13 @RestController
    14 public class SampleControler {
    15 
    16     @RequestMapping("/test/home")
    17     public String home() {
    18         return "xdclass";
    19     }
    20 
    21 }

        测试:

        MockMvcTestDemo.java:

     1 package xdclass_springboot.demo;
     2 
     3 import net.xdclass.demo.XdClassApplication;
     4 
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
     9 import org.springframework.boot.test.context.SpringBootTest;
    10 import org.springframework.test.context.junit4.SpringRunner;
    11 import org.springframework.test.web.servlet.MockMvc;
    12 import org.springframework.test.web.servlet.MvcResult;
    13 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    14 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    15 
    16 
    17 
    18 /**
    19  * 功能描述:测试mockmvc类
    20  */
    21 @RunWith(SpringRunner.class)  //底层用junit  SpringJUnit4ClassRunner
    22 @SpringBootTest(classes={XdClassApplication.class}) //启动整个springboot工程
    23 @AutoConfigureMockMvc 
    24 public class MockMvcTestDemo {
    25 
    26     
    27     @Autowired
    28     private MockMvc mockMvc;
    29     
    30     
    31     
    32     @Test
    33     public void apiTest() throws Exception {
    34         
    35         MvcResult mvcResult =  mockMvc.perform( MockMvcRequestBuilders.get("/test/home_xxx") ).
    36                 andExpect( MockMvcResultMatchers.status().isOk() ).andReturn();
    37         int status = mvcResult.getResponse().getStatus();
    38         System.out.println(status);
    39         
    40     }
    41     
    42 }

    1、@SpringBootTest单元测试实战简介:讲解SpringBoot的单元测试1、引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>

    2、使用@RunWith(SpringRunner.class)  //底层用junit  SpringJUnit4ClassRunner@SpringBootTest(classes={XdclassApplication.class})//启动整个springboot工程public class SpringBootTests { }

  • 相关阅读:
    20款时尚的 WordPress 博客主题【免费下载】
    垂涎欲滴!30个美味的食品类移动应用程序【上篇】
    Skippr – 轻量、快速的 jQuery 幻灯片插件
    Boba.js – 用于 Google 统计分析 JavaScript 库
    长期这么做的后果就是人民劳苦而得不到该有的回报,怎么能不垮
    左值与右值的根本区别在于能否获取内存地址,而能否赋值不是区分的依据
    百度后端C++电话一面
    Web 开发和数据科学家仍是 Python 开发的两大主力
    Consul架构
    去除两端逗号-JS
  • 原文地址:https://www.cnblogs.com/116970u/p/10223724.html
Copyright © 2011-2022 走看看