【问题标题】:Tests not running through Maven?测试不通过 Maven 运行?
【发布时间】:2017-09-22 08:58:20
【问题描述】:

当我在 Maven 中运行测试时,我得到了这个:

[INFO] -------------------------------------------------------    
[INFO]  T E S T S     
[INFO] -------------------------------------------------------   
[INFO]   
[INFO] Results:  
[INFO]   
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

我的测试类 JsonReaderTest.class 放置在 src/test/java 中,据我从 maven-surefire-plugin 了解,它遵循正确的命名约定。

在 Maven 之外运行时测试运行良好。

我的 pom 中包含了这个插件:

<!-- Executes tests -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
</plugin>

这在我的依赖中:

<!-- Test -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.0</version>
</dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.0.0</version>
</dependency>

还有我的测试课:

package org.avalin.optaplanner.test.java;

import org.avalin.optaplanner.json.JsonReader;
import org.junit.jupiter.api.*;

import java.io.FileNotFoundException;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.*;

public class JsonReaderTest
{
    @Test
    @DisplayName("Test: No such file at designated path")
    void testloadFromJsonTest() throws Exception
    {
        Throwable exception = assertThrows(FileNotFoundException.class, 
        ()-> JsonReader.loadFromJson(".json"));
        assertEquals(".json (No such file or directory)", 
        exception.getMessage());
    }

    @Test
    @DisplayName("Test: Load Shifts from JSON (String instead of number)")
    void testLoadShiftsFromJson3()
    {
        Throwable exception = assertThrows(NumberFormatException.class, ()-> JsonReader.loadFromJson(Paths.get("src/main/resources/org/avalin/optaplanner/json/faultyShift-2.json").toAbsolutePath().toString()));
        assertEquals("\nOne or more of your \"shift\" elements has a number format exception.\n" +
            "Check for errors in your JSON-properties.\n" +
            "(Did you insert a string instead of a number in id?)", 
        exception.getMessage());
    }

    @Test
    @DisplayName("Test: JSON is correctly loaded")
    void testJsonIsLoaded()
    {
        assertFalse(JsonReader.jsonIsLoaded());
    }

    @AfterEach
    void cleanJsonReader()
    {
        JsonReader.cleanJsonReader();
    }
}

当我尝试用谷歌搜索这个问题时,似乎唯一可能出错的是命名约定(类必须以 test 结尾或以 test 开头,我测试了两者都没有改变)并且应该将测试类放入相应的文件夹。

当我运行时:mvn -Dtest=JsonReaderTest test

我得到以下信息:

Failed to execute goal org.apache.maven.plugins:maven-surefire-
plugin:2.20.1:test (default-test) on project optaplanner: No tests were 
executed!  

JsonReaderTest.class 也在 target/test-classes 中正确生成

这里的罪魁祸首可能是什么?

【问题讨论】:

  • 您尝试使用哪个版本的junit?从您的测试类来看,在我看来您想使用 junit 5,但从您的 Maven 配置来看,似乎适用于 junit 4。
  • 我在你的测试课中看不到'package'行?你的测试类在默认包中吗?
  • 我尝试重新创建您的示例。我收到此错误:initializationError(package.maven.MyTest): The class package.maven.MyTest is not public. 您可以尝试在您的课程中添加公共修饰符吗?

标签: java maven unit-testing junit junit5


【解决方案1】:

同时使用 Maven Surefire 插件和 JUnit 5 需要一些调整...

来自the docs

JUnit 团队为 Maven Surefire 开发了一个非常基本的提供程序,可让您通过 mvn test 运行 JUnit 4 和 JUnit Jupiter 测试。 junit5-maven-consumer 项目中的 pom.xml 文件演示了如何使用它,可以作为一个起点。

由于 Surefire 2.20 中的内存泄漏,junit-platform-surefire-provider 目前仅适用于 Surefire 2.19.1。

...
<build>
    <plugins>
        ...
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
...

【讨论】:

  • 从上面的文档链接中,不再推荐这样做:“最初由 JUnit 团队开发的 junit-platform-surefire-provider 在 JUnit Platform 1.3 中已弃用,并在 1.4 中停止使用。请改用 Maven Surefire 的原生支持。”
【解决方案2】:

这个插件对我有用:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>3.0.0-M3</version>
                    </dependency>
                </dependencies>
            </plugin>

取自https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

【讨论】:

    【解决方案3】:

    以下 pom 配置对我有用:

    <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <version>5.4.0</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>5.4.0</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.junit.platform</groupId>
          <artifactId>junit-platform-launcher</artifactId>
          <version>1.4.0</version>
          <scope>test</scope>
      </dependency>
    ....
    <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0</version>
                </dependency>
            </dependencies>
        </plugin>
    ...
    

    上述@glytching 的插件部分

    【讨论】:

    • 不错!加上junit-jupiter-engine就够了
    • 这个,除了没有测试范围的依赖,解决了我的问题
    【解决方案4】:

    我遇到了同样的问题。所有的测试类都是包私有的,JUnit 5 看不到它们。解决方案是将此依赖项添加到 pom 文件中:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.2</version>
        <scope>test</scope>
    </dependency>
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,我花了好几天才找到解决方案! 首先,确保以***Test.java 结束您的类名,例如:MyGreatTest.java 其次,公开你的类和方法!这很重要,否则mvn test 会忽略你的测试课! 第三,我在 pom.xml 中添加了这段代码:

      <build>
          <plugins>
              <plugin>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.22.0</version>
              </plugin>
          </plugins>
      </build>
      

      【讨论】:

      • 这在 JUnit 4 和 Junit 5 之间发生了变化:测试类、测试方法和生命周期方法不再需要是公共的,但它们不能是私有的。所以你可以省略'public'修饰符,所以它们使用默认可见性(本地包)。
      • 不,不是。如果您没有指定公共或私有,它被认为是包本地的,JUnit 5 仍然会找到您的测试。参见例如:github.com/junit-team/junit5-samples/blob/r5.5.1/…
      猜你喜欢
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 2014-06-21
      • 2011-01-06
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多