测试没有通过Maven运行?

时间:2017-09-22 08:58:20

标签: java maven unit-testing junit junit5

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

当我尝试使用Google搜索这个问题时,似乎唯一可能是错误的是命名约定(类必须以测试结束或者以测试开始,我测试两者都没有更改)并且测试类应该放入适当的文件夹。

当我跑步时: 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!  

JsonReaderTest.class也在target / test-classes

中正确生成

这可能是罪魁祸首?

4 个答案:

答案 0 :(得分:14)

使用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>
...

答案 1 :(得分:1)

以下pom配置对我有用:

size

上述@glytching插件部分

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

此插件对我有用:

            <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