Springboot测试类之@RunWith注解

  • Springboot测试类之@RunWith注解

Springboot测试类之@RunWith注解

@runWith注解作用:

  • @RunWith就是一个运行器
  • @RunWith(JUnit4.class)就是指用JUnit4来运行
  • @RunWith(SpringJUnit4ClassRunner.class),让测试运行于

Spring测试环境,以便在测试开始的时候自动创建Spring的应用上下文
–@RunWith(Suite.class)的话就是一套测试集合

引申:
Spring Boot 1.5.2 Junit测试
使用 Spring 进行单元测试

方法1:【参考文献】

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class BBTestAA {@Autowiredprivate TestRestTemplate testRestTemplate;//Application.class 为SpringBoot的启动入口类,每个SpringBoot项目大家都会配置
}

如果pom.xml中有如下代码,这行@RunWith(SpringRunner.class)就不会出现SpringRunner,反而有@RunWith(SpringJUnit4ClassRunner.class)

<!--spring-test测试=-->
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version>
</dependency>
如果pom.xml中没有这段,则@RunWith(SpringRunner.class)不会报错。如果有这段:①未注释<scope>test</scope>会报错;②注释<scope>test</scope>不会报错
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>
如果pom.xml中没有这段,则会报错。如果有这段:①未注释<scope>test</scope>SpringRunner、SpringBootTest无法引用,会报错;②注释<scope>test</scope>不会报错
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.9.RELEASE</version><scope>test</scope>
</dependency>

总结起来,想要使用

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
pom.xml中应该引用这两个

<!--spring-test测试=--><!--<dependency>--><!--<groupId>org.springframework</groupId>--><!--<artifactId>spring-test</artifactId>--><!--<version>4.2.4.RELEASE</version>--><!--</dependency>--><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>1.5.9.RELEASE</version><!--<scope>test</scope>--></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><!--<scope>test</scope>--></dependency>

方法2:【参考文献】
如果有test@RunWith报红,没有test会引入该类

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>

如果有test@SpringBootTest报红,没有test会引入该类

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>1.5.9.RELEASE</version><scope>test</scope>
</dependency>

如果是4.2.4.RELEASESpringRunner报红,如果4.2.4.RELEASE会引入该类

org.springframework spring-test 4.2.4.RELEASE 所以最后要正确使用,需引入这些架包

  <!--spring-test测试=--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>1.5.9.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>

2.在IDE中新增JunitTest类

@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class
@SpringBootTest(classes = Application.class) //1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class)
public class SystemInfoServiceImplTest {@Autowiredprivate ISystemInfoService systemInfoservice;@Testpublic void add() throws Exception {}@Testpublic void findAll() throws Exception {}}

主要是注解的更改,如果注解用的不对,会报各种奇怪的问题,例如applicationContext找不到,datasource实例化失败等等。

为了支持上面两个注解,maven文件中要用对依赖以及版本,我当时添加SpringRunner.class所在的依赖jar时,由于用了ideaauto-imported,IDE自动导入了版本是3.x的,实际应该导入4.x,我一直以为idea导入的是正确的,导致在这上面费时颇多,后来我手工写入就解决了。下面是正确的spring boot testmaven依赖。

【参考文献】

天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解相关推荐

  1. Springboot测试类之@RunWith注解

    @runWith注解作用: --@RunWith就是一个运行器 --@RunWith(JUnit4.class)就是指用JUnit4来运行 --@RunWith(SpringJUnit4ClassRu ...

  2. SpringBoot - 应用程序测试方案

    文章目录 Pre Spring Boot 中的测试解决方案 测试 Spring Boot 应用程序 初始化测试环境 @SpringBootTest @SpringBootTest - webEnvir ...

  3. springboot集成swagger2测试接口

    springboot集成swagger2测试接口 1.需要的依赖 2.开始编写一个swagger2 3.演示效果图片 1.需要的依赖 <dependency><groupId> ...

  4. 又到了上云时刻啦!!!阿里云 Docker部署SpringBoot项目 方便测试的部署方式

    又到了上云时刻啦!!!阿里云 Docker部署SpringBoot项目 方便测试的部署方式 部署思路: 具体步骤: 1.打成jar 2.上传到服务器 3.启动JDK容器 4.进入JDK 容器 启动 项 ...

  5. SpringBoot在使用测试的时候是否需要@RunWith?

    我们在使用SpringBoot进行测试的时候一般是需要加两个注解: @SpringBootTest 目的是加载ApplicationContext,启动spring容器. @RunWith 是一个测试 ...

  6. SpringBoot整合ElasticSearch测试代码

    1.SpringBoot环境和测试类,我就不讲了. 2.yml中的配置 clustername:是es的集群名称,默认是elasticsearch cluster-nodes:tcp的地址,不要写错. ...

  7. 基于java(springboot)大学生体质测试管理系统(java毕业设计)

    基于java(springboot)大学生体质测试管理系统 大学生体质测试管理是基于java编程语言,mysql数据库,springboot框架,idea开发工具进行开发,本系统分为学生,管理员,教师 ...

  8. Java运行非Web的Springboot项目(测试类或启动主类两种方法)

    Java运行非Web的Springboot项目(测试类或启动主类两种方法) 前言 一.创建打开一个Springboot项目 二.运行项目 1.Test测试类运行 2.启动主类运行 三.持续运行不停止( ...

  9. SpringBoot集成JUnit测试

    在一些企业的实践中,要求开发人员编写测试编码来测试业务逻辑,以提高编码的质量.降低错误的发生概率以及进行性能测试等.这些IDE在创建Spring Boot应用的时候已经引入了测试包,只需要看到pom. ...

最新文章

  1. 扫盲篇:用户体验不等于可用性
  2. 【PC工具】更新速度最快最好用的文件内容搜索工具:searchmyfiles
  3. Acwing第 22 场周赛【未完结】
  4. php 实现两变量值对换,php两个变量的值交换的方法
  5. Windows上使用Git管理文件
  6. 京东金融创新”ABS云平台” 大数据提升直接融资效率
  7. 利用TP-Link+花生壳建立属于自己的网站
  8. 笔记本主板如何测试软件,笔记本硬件检测,小编教你笔记本怎么检测硬件
  9. C2872 “detail”: 不明确的符号
  10. PHP四端代码,壹脉销客智能名片,全套四端开源代码
  11. 10php1c,很急的一個問題,請幫幫忙
  12. mac 下载软件无法将程序拖移至app 出现错误
  13. 微信小程序超级占内存_实测:微信小程序占多少内存?或许真没有你想象的那么“小”!...
  14. OpenCascade学习笔记-创建一个简单的OpenCascade单文档
  15. 深度解读《中国智能经济发展白皮书》:AI为核心驱动力,百度给出智能范式
  16. 少儿Python编程6-计算机数据结构和算法
  17. Java读.xlsx表格
  18. JS网页更改背景颜色源代码
  19. 第一天在这里发布 爱客CRM的博客
  20. ScheduledThreadPoolExecutor源码分析

热门文章

  1. 中国自动浓咖啡机市场趋势报告、技术动态创新及市场预测
  2. 超几何分布与二项分布及其期望
  3. 社会力模型SFM详解 在人群异常检测上的应用
  4. 基于百度地图实现Android定位功能实现(详解+教程)
  5. 【AliOS Studio】AliOS Studio初体验
  6. 程序“[4176] char.exe: 本机”已退出,返回值为 0 (0x0)
  7. BZOJ3168. 【HEOI2013】钙铁锌硒维生素
  8. SEO与SEM区别是什么
  9. 在家干什么副业好?工作之余做什么副业比较好
  10. 汇编c语言基础教程编程达人,编程达人 《汇编、C语言基础教程》第一章 进制1.6 逻辑运算(连载)...