springboot的单元测试(总结两种)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); border-top-left-radius: 7px; border-top-right-radius: 7px; color: rgba(255, 255, 255, 1); height: 1.8em; line-height: 1.8em; padding: 5px }
springboot的单元测试,这里介绍两种方式,一种是在测试类中添加注解;另一种是在启动的main方法中继承CommandLineRunner接口(也可以写在其他方法中)。
如 对查看数据库的连接池信息 进行单元测试
1. 在类上使用注解:
@RunWith(SpringRunner.class)
@SpringBootTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class RobotsApplicationTests { @Autowired
DataSource dataSource; @Test
public void test(){
System.out.println(dataSource.getClass());
} }
2. 继承CommandLineRunner接口
CommandLineRunner:表示在项目启动完成后 会执行该功能,只需将测试的内容写在其run()方法中,如:
@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages={"com.cmit.hall.plat","com.cmit.hall.pub"})
@ServletComponentScan(value= {"com.cmit.hall.pub.interceptor","com.cmit.hall.plat.config","com.cmit.hall.pub.session"})
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=1800)
public class PlatApp implements CommandLineRunner { @Autowired
DataSource dataSource; public static void main(String[] args) {
SpringApplication.run(PlatApp.class, args);
} @Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");
System.out.println("DATASOURCE = " + dataSource);
}
}
springboot的单元测试(总结两种)的更多相关文章
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- Springboot中IDE支持两种打包方式,即jar包和war包
Springboot中IDE支持两种打包方式,即jar包和war包 打包之前修改pom.xml中的packaging节点,改为jar或者war 在项目的根目录执行maven 命令clean pa ...
- 【SpringBoot】05.SpringBoot整合Listener的两种方式
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...
- 【SpringBoot】03.SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...
- SpringBoot单元测试的两种形式
@ 目录 前言 demo环境 springbootTest Junit 总结 前言 最近公司要求2021年所有的项目代码单元测试覆盖率要达到90%,作为刚毕业的小白来说这简直就是噩梦啊,springb ...
- springboot之jackson的两种配置方式
springboot 针对jackson是自动化配置的,如果需要修改,有两种方式: 方式一:通过application.yml 配置属性说明:## spring.jackson.date-format ...
- springboot整合mybatis的两种方式
https://blog.csdn.net/qq_32719003/article/details/72123917 springboot通过java bean集成通用mapper的两种方式 前言:公 ...
- SpringBoot热部署的两种方式
SpringBoot热部署方式一共有两种,分别使用两种不同的依赖 SpringBoot 1.3后才拥有SpringBoot devtools热部署 ①:spring-boot-devtools ② ...
- 【SpringBoot】04.SpringBoot整合Filter的两种方式
SpringBoot整合Filter过滤器的两种方式: 1.通过扫描注解完成Filter组件注册 创建一个类,实现Filter接口,实现doFilter()方法 在该类使用注解@WebFilter,设 ...
随机推荐
- Java数据库开发(三)之——补充
一.SQL注入与防范 使用PreparedStatement替代Statement对象,它提供了参数化SQL的方式 二.事务 定义 事务是并发控制的基本单位,满足ACID特征 原子性:atomicit ...
- BUU mrctf shit
吐槽:去年没写出的题,现在终于可以上手了,昂哥nb 动调发现直接卡着不动了,怀疑是反调试,果然有好几处反调试 这里选择就不先nop了,先让程序跑起来,然后attach,在输入函数下面下个断点,atta ...
- 深入理解 PHP7 中全新的 zval 容器和引用计数机制
深入理解 PHP7 中全新的 zval 容器和引用计数机制 最近在查阅 PHP7 垃圾回收的资料的时候,网上的一些代码示例在本地环境下运行时出现了不同的结果,使我一度非常迷惑. 仔细一想不难发现问题所 ...
- Message /index.jsp (line: [17], column: [45]) The JSP specification requires that an attribute name is preceded by whitespace
Error: Message /index.jsp (line: [17], column: [45]) The JSP specification requires that an attribut ...
- Windows 上连接蓝牙耳机
"开始"菜单 –> 输入蓝牙 点击蓝牙设备,选择连接设备即可.
- Xshell怎么连接数据库
之前一直用Navicat Premium链接数据库,其实在xshell也可以链接数据库,本文将先介绍如何用xshell链接数据库的方法. 1.打开xshell,连接上 输入指令:mysql -h 19 ...
- C语言:最大公约数和最小公倍数
#include <stdio.h> int main() { int a,b,c,m,t; printf("请输入两个数:\n"); scanf("%d%d ...
- C语言:char[]操作
C语言中char[]的操作函数1.赋值操作在C语言中,char型数组是不可以直接赋值的.例如在如下的代码中,会得到错误: char c1[20] = "women";char c2 ...
- DEV C++5.11编译没有结果提示
点击"视图"菜单--选择"浮动报告 窗口"
- 【论文阅读】Socially aware motion planning with deep reinforcement learning-annotated
目录 摘要部分: I. Introduction 介绍 II. Background 背景 A. Collision Avoidance with DRL B. Characterization of ...