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:

 package net.xdclass.demo.controller;

 import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*; import net.xdclass.demo.domain.User; @RestController
public class SampleControler { @RequestMapping("/test/home")
public String home() {
return "xdclass";
} }

    测试:

    MockMvcTestDemo.java:

 package xdclass_springboot.demo;

 import net.xdclass.demo.XdClassApplication;

 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.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; /**
* 功能描述:测试mockmvc类
*/
@RunWith(SpringRunner.class) //底层用junit SpringJUnit4ClassRunner
@SpringBootTest(classes={XdClassApplication.class}) //启动整个springboot工程
@AutoConfigureMockMvc
public class MockMvcTestDemo { @Autowired
private MockMvc mockMvc; @Test
public void apiTest() throws Exception { MvcResult mvcResult = mockMvc.perform( MockMvcRequestBuilders.get("/test/home_xxx") ).
andExpect( MockMvcResultMatchers.status().isOk() ).andReturn();
int status = mvcResult.getResponse().getStatus();
System.out.println(status); } }

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 { }

SpringBootTest单元测试实战、SpringBoot测试进阶高级篇之MockMvc讲解的更多相关文章

  1. 小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战

    笔记 1.@SpringBootTest单元测试实战     简介:讲解SpringBoot的单元测试         1.引入相关依赖              <!--springboot程 ...

  2. 单元测试实战 - Junit测试

    一.对加法函数进行测试 1.实例化被测单元(方法):类名 实例名=new 类名([参数]) 2.调用被测单元,对比预期值和输出值(实际值): 在没有junit测试工具的情况下,我们要进行如下的测试代码 ...

  3. OpenFaaS实战之九:终篇,自制模板(springboot+maven+jdk8)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. 【mongoDB高级篇③】综合实战(1): 分析国家地震数据

    数据准备 下载国家地震数据 http://data.earthquake.cn/data/ 通过navicat导入到数据库,方便和mysql语句做对比 shard分片集群配置 # step 1 mkd ...

  5. springboot测试启动报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    springboot测试启动报错: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you ne ...

  6. SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...

  7. 在Eclipse中使用JUnit4进行单元测试(初级篇、中级篇、高级篇)

    本文转载自以下 初级篇: http://blog.csdn.net/andycpp/article/details/1327147 中级篇: http://blog.csdn.net/andycpp/ ...

  8. 从零开始学Axure原型设计(高级篇)

    如果你熟悉了Axure的部件库,那么你可以得心应手地画出心目中产品的线框图:如果你会用Axure的母版.动态面板功能,那么你应该能够画出一些简单网站的原型图:但只有你精通了Axure的条件逻辑.变量. ...

  9. SpringBootTest单元测试及日志

    springboot系列学习笔记全部文章请移步值博主专栏**: spring boot 2.X/spring cloud Greenwich. 由于是一系列文章,所以后面的文章可能会使用到前面文章的项 ...

随机推荐

  1. Spring的编程式事务和声明式事务

    事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务管理的两种方式 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务使用Transa ...

  2. 【ZOJ2277】The Gate to Freedom

    BUPT2017 wintertraining(16) #4 E ZOJ - 2277 题意 输出\(n^n\)的首位的数字. 题解 用科学计数法表示\(n^n=k\cdot 10^b\),那么\(n ...

  3. 自学Linux Shell10.1-使用编辑器vim

    点击返回 自学Linux命令行与Shell脚本之路 10.1-使用编辑器vim 所有的 Unix系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在.但是目前我们使用比较多的是 vim 编辑 ...

  4. [luogu4513]小白逛公园

    题目描述 在小新家附近有一条"公园路",路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了. 一开始,小白就根据公园的风景给每个公园打了分-.-.小新 ...

  5. SP1805 HISTOGRA (单调栈)

    单调栈维护栈顶为高度最大的 记下来栈中每个元素入栈时顶掉的最靠左的一个位置(如果没顶掉就是它本身),那么在它出栈的时候,它所带来的面积就是(出栈位置-记录位置)*高度 (可能会有加一减一之类的细节) ...

  6. 树莓派上使用Pi-FM-RDS工具打造FM调频电台

    安装Pi-FM-RDS 安装依赖.sudo apt-get install libsndfile1-dev 克隆Pi-FM-RDS到本地.git clone https://github.com/Ch ...

  7. 【BZOJ2648】SJY摆棋子

    题目大意:维护一个二维平面,平面上初始有 N 个点,支持两种操作:平面加点.查询距离某个指定点的最小哈密顿距离. 题解:学习到了 kd-tree 数据结构. kd-tree 类似于平衡树,即:每个节点 ...

  8. 【bzoj3680】平衡点 模拟退火

    模拟退火是一种求函数最值问题的随机算法. 给定一个函数的某一初始坐标,可以拟定一个"温度"(这里主要是借用退火的物理意义),这里的温度可以理解成自变量可以取值的范围.之后在当前最优 ...

  9. MATLAB:图像的移动(move函数)

    图像移动涉及到move函数,实现过程如下: close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clear all; clc; I=imread('lenna.bmp ...

  10. SVN:多版本库环境的搭建

    一. 1,启动SVN sudo svnserve -d -r /home/data/svn/ 其中 -d 表示守护进程, -r 表示在后台执行 /home/data/svn/  为svn的安装目录 2 ...