不通过 Maven 测试 运行?

Tests not running through Maven?

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

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

我的测试 class、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>

和我的测试class:

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();
    }
}

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

当我运行: mvn -Dtest=JsonReaderTest 测试

我得到以下信息:

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

在target/test-classes

里面也正确生成了JsonReaderTest.class

这里的罪魁祸首是什么?

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

来自the docs

The JUnit team has developed a very basic provider for Maven Surefire that lets you run JUnit 4 and JUnit Jupiter tests via mvn test. The pom.xml file in the junit5-maven-consumer project demonstrates how to use it and can serve as a starting point.

Due to a memory leak in Surefire 2.20, the junit-platform-surefire-provider currently only works with 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 5 看不到它们。解决方案是将此依赖项添加到 pom 文件中:

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

以下 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所述的插件部分

这个插件对我有用:

            <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

我遇到了同样的问题,我花了几天时间才找到解决方案! 首先,请确保您的 class-name 以 ***Test.java 结尾,例如:MyGreatTest.java 其次,让你的class和方法public!这一点很重要,否则mvn test会忽略你的测试class! 第三,我在 pom.xml:

中添加了这段代码
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
    </plugins>
</build>