ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Springboot测试类之@RunWith注解

2020-10-22 17:03:22  阅读:271  来源: 互联网

标签:RunWith Springboot -- spring RELEASE test 注解 class


@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 {
    @Autowired
    private 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 {

        @Autowired
        private ISystemInfoService systemInfoservice;

        @Test
        public void add() throws Exception {
        }

        @Test
         public void findAll() throws Exception {
         }

}
复制代码

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

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

标签:RunWith,Springboot,--,spring,RELEASE,test,注解,class
来源: https://www.cnblogs.com/xiami2046/p/13859334.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有