代码之家  ›  专栏  ›  技术社区  ›  Carmageddon

找不到测试-在bare bone Spring Boot Maven项目上运行jUnit 5测试用例时测试套件为空

  •  0
  • Carmageddon  · 技术社区  · 8 年前

    所以我们得到消息了

    Process finished with exit code 0
    Empty test suite.
    

    在我们的多模块项目上尝试运行Junit5 testscases时,为了测试问题的来源,我决定从头开始尝试一个新项目(我使用的是IntelliJ IDEA 2018.2.4)。

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.org.bu.dep.app</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
            <relativePath/>
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>2.0.5.RELEASE</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>com.vaadin.external.google</groupId>
                        <artifactId>android-json</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.3.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>5.3.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    

    我尝试了与junit vintage引擎相同的结果。

    下面是测试类:

    package com.org.bu.dep.app.demo;
    
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    public class DemoApplicationTests {
    
        @Test
        public void contextLoads() {
            Assertions.assertTrue(true);
        }
    
    }
    

    在跑步窗口,上面写着 No tests were found

    @SpringBootTest 我试过了 @ExtendWith(SpringExtension.class)

    我已经在google上搜索过了,大多数结果/答案似乎都来自2016/2017年的IntelliJ旧版本,这些版本与junit5有问题,似乎没有关联。

    如果我们花太多时间在它上面,它将被迫恢复到4,我非常不希望这样做:)

    3 回复  |  直到 8 年前
        1
  •  14
  •   Carmageddon    8 年前

    所以,对于Junit 5和Spring Boot 2来说,以下3个因素是有效的:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
    

    用空项目进行测试和验证。

    请注意@ Michael Legart's 建议添加的答案

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.1</version>
    </plugin>
    

    是maven test(和Jenkins)检测和执行测试所必需的!

        2
  •  13
  •   Luís Soares Cilan    6 年前

    private 试验方法-应 public

        3
  •  4
  •   Michael Legart    8 年前

       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>2.22.1</version>
       </plugin>
    

    JUnit5支持是在2.22.0中添加的,请参见 https://issues.apache.org/jira/browse/SUREFIRE-1330

        4
  •  4
  •   garg10may    6 年前

    对我有用的东西。

    junit-bom

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.6.1</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
    

    也与 maven-surefire-plugin 不添加版本

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        5
  •  1
  •   Guy Coder    8 年前

    因为你用的是Intellij。你把你的名字记下来了吗 java test folder 作为 Test Sources Root

    enter image description here

        6
  •  1
  •   ozinal    6 年前

    add dependency

    添加' 数据库附加工具:rider-junit5:1.2.5 '

    enter image description here

    ' 数据库附加工具:rider-junit5:1.2.5

        7
  •  0
  •   Robse    5 年前

    我知道这是一个老问题,但也许它能帮助别人。 我收到错误消息是因为我从声明方法的临时文件中复制粘贴了一个方法 static .

        8
  •  -1
  •   Pavan Ulichi    5 年前

    使用IntelliJ,我必须删除目标文件夹并运行测试来消除这个错误