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. BZOJ3453 XLkxc(拉格朗日插值)

    显然f(i)是一个k+2项式,g(x)是f(i)的前缀和,则显然其是k+3项式,插值即可.最后要求的东西大胆猜想是个k+4项式继续插值就做完了.注意2p>maxint…… #include< ...

  2. KEYENCE Programming Contest 2019 自闭记

    A:签到. #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> ...

  3. bzoj 2202 [HNOI2010] Bounce 弹飞绵羊(分块)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 思路:和之前那道树分块的题很像,只不过那道是在树上,这道简单些在序列上 还是维护两个数 ...

  4. Linux 下 wordpress 无法安装插件

    修改目录权限mkdir -p wp-content/tmpchown -R www:www  wp-contentchmod -R 777 wp-content 配置修改wp-config.php搜索 ...

  5. 【BZOJ4738/UOJ#276】汽水(点分治,分数规划)

    [BZOJ4738/UOJ#276]汽水(点分治,分数规划) 题面 BZOJ UOJ 题解 今天考试的题目,虽然说是写完了,但是感觉还是半懂不懂的来着. 代码基本照着\(Anson\)爷的码的,orz ...

  6. bzoj1001/luogu4001 狼抓兔子 (最小割/平面图最小割转对偶图最短路)

    平面图转对偶图:先在原图中加一个s->t的边,然后对每个面建一个点,对每条分隔两个面的边加一条连接这两个面对应点的边,边权等于原边权. 然后从刚才加的s->t分割出来的两面对应的两个点跑最 ...

  7. Centos6.5之ssh免密码登录配置

    Centos6.5之ssh免密码登录配置 centos ssh 免密码登录 0.说明 这里为了方便说明问题,假设有A和B两台安装了centos6.5的主机.目标是实现A.B两台主机分别能够通过ssh免 ...

  8. ASP:连接Access数据库的方法及使用感受

    连接Access的方式有: 方式1: Set conn = Server.Createobject("ADODB.Connection")conn.open "drive ...

  9. ZOJ_3950_How Many Nines 解题报告及如何对程序进行测试修改

    The 17th Zhejiang University Programming Contest Sponsored by TuSimple Solution: #include <stdio. ...

  10. MyEclipse导入Maven项目以及Maven转化为Dynamic Web Module(转)

    MyEclipse导入Maven项目(这个博主写的比较详细)https://blog.csdn.net/w12345_ww/article/details/52094756 Maven转化为Dynam ...