zoukankan      html  css  js  c++  java
  • SpringBoot(2.2.X)单元测试遇到的坑——Junit测试框架

    使用IDEA创建Spring Initializr初始化带有web的Spring Boot 2.2.X版本的pom文件如下:

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        
        <groupId>com.example</groupId>
            <artifactId>demo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
          </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    此时默认的是JUnit5,网上很多Spring Boot2.X教程使用的是Junit4

    所以在测试类的时候会发现无法引入@RumWith(SpringRunner.class),网上搜基本都是什么jar不对,需要引入哪几个jar包,然并卵....

    其实只要把spring-boot-starter-test中的exclusions删除或注释掉就好了

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    改成

       <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
    <!--            <exclusions>-->
    <!--                <exclusion>-->
    <!--                    <groupId>org.junit.vintage</groupId>-->
    <!--                    <artifactId>junit-vintage-engine</artifactId>-->
    <!--                </exclusion>-->
    <!--            </exclusions>-->
            </dependency>
  • 相关阅读:
    刷题86—动态规划(三)
    刷题85—动态规划(二)— 股票6道
    刷题84—动态规划(一)
    刷题83——硬币
    刷题82——二叉树的右视图
    刷题81——统计「优美子数组」
    android adb 流程原理代码分析(一)
    android默认开启adb调试方法分析
    recovery 下界面UI旋转90 180 270修改
    sublime使用Package Control不能正常使用的解决办法
  • 原文地址:https://www.cnblogs.com/wx60079/p/12539648.html
Copyright © 2011-2022 走看看