@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:
如果有<scope>test</scope>@RunWith报红,没有<scope>test</scope>会引入该类

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

如果有<scope>test</scope>@SpringBootTest报红,没有<scope>test</scope>会引入该类

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

如果是<version>4.2.4.RELEASE</version>SpringRunner报红,如果<version>4.2.4.RELEASE</version>会引入该类

<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version>
</dependency>

所以最后要正确使用,需引入这些架包

<!--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实例化失败等等。

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

Springboot测试类之@RunWith注解相关推荐

  1. 天呐!惊人的Springboot测试.Springboot测试类之@RunWith注解

    Springboot测试类之@RunWith注解 Springboot测试类之@RunWith注解 Springboot测试类之@RunWith注解 @runWith注解作用: @RunWith就是一 ...

  2. SpringBoot测试类不需要加@RunWith?

    # 在idea中,自动生成的测试类没有@RunWith运行正常? 在SpringBoot2.2.0以前是JUnit4,在SpringBoot之后是JUnit5. Juint版本说明 Junit4 &l ...

  3. springboot(测试类)整合mybatisplus增删改查

    文章目录 下载地址 1.创建springboot项目(Spring Initializer快速创建) 2.配置文件,数据库等 3.配置,策略,实体类 4.增删改查 查询 查询全部 查询-根据id批量查 ...

  4. SpringBoot启动类的扫描注解的用法及冲突原则

    背景 SpringBoot 启动类上,配置扫描包路径有三种方式,最近看到一个应用上三种注解都用上了,代码如下: @SpringBootApplication(scanBasePackages ={&q ...

  5. Springboot测试类的编写

    1.首先看看Controller写的一些东西 @RestController public class HelloController {@RequestMapping("/hello&qu ...

  6. Springboot实体类配置索引注解

    普通索引 name是索引名称(自定义的),columnList是字段名 @Entity @Table(name = "dm_question", indexes = {@Index ...

  7. SpringBoot配置类替代@Transactional注解实现事务的控制

    package com.example.zcw.config;import org.aspectj.lang.annotation.Aspect; import org.springframework ...

  8. java单元测试启动类配置_Springboot 单元测试简单介绍和启动所有测试类的方法

    最近一段时间都是在补之前的技术债,一直忙着写业务代码没有注重代码的质量,leader也在强求,所有要把单元测试搞起来了 我把单元测试分为两种 一个是service的单元测试,一个是controller ...

  9. spring、springboot项目测试类遇到的问题

    常见问题: 1.junit注解无法使用 junit依赖中添加了<scope>test</scope>junit中的注解需要在项目中src下的test文件夹(如删了或没有需要自己 ...

最新文章

  1. 阿里二面,原来我对自动化测试的理解太浅了
  2. 电灯泡内通有交流电,为什么看不到灯泡在闪烁?
  3. NPTL简介 (NATIVE POSIX Thread Library)
  4. TCP/IP详解--TCP/IP中三次握手 四次握手状态分析
  5. sqllite开发安卓项目_苹果安卓合作了?两大巨头共同居然共同开发这个项目
  6. centosx64 6.2安装virtualbox
  7. mysql第二天无法连接_jdbc 连接mysql 第二天就连接不成功,什么原因?-问答-阿里云开发者社区-阿里云...
  8. [面试] C/C++ 语法(五) —— extern
  9. 使用Git将本地文件夹同步至github
  10. 【Java程序设计】多线程基础
  11. 计算机科学引论2答案,计算机科学引论答案-20210311090508.docx-原创力文档
  12. linux系统怎么启动服务器,Linux操作系统的启动步骤详细说明
  13. Ipmonitor9迁移安装后认证不可用的问题
  14. 【牛客网-公司真题-前端入门篇】——百度2021校招Web前端研发工程师笔试卷(第三批)
  15. html文件下载时的header设置
  16. 一文带你了解dfs和bfs算法
  17. FinalShell连接超时解决方法
  18. python判断按键是否按下_python – 如何检查键修饰符是否被按下(shift,ctrl,alt)?
  19. php订阅号网页登录,微信订阅号怎么使用网页授权登录
  20. 人物-商界-杨惠妍:杨惠妍

热门文章

  1. 自动驾驶数据闭环系列之一:理想丰满,现实骨感
  2. Linux网络编程(四)
  3. 「TYVJ1017」冗余关系
  4. iPad浏览器HTML5性能测试
  5. 宝宝成长季4天-我出生啦!
  6. 写给女朋友的java_Java会说情话的女朋友
  7. jQuery+PHP+Ajax动态数字统计展示实例
  8. 区块链是什么,如何简单易懂地介绍区块链
  9. 支付宝app支付对接1
  10. 【zyc的从零开始】20211012 运算符