测试未通过Maven运行? [英] Tests not running through Maven?

查看:91
本文介绍了测试未通过Maven运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I run my test in Maven I get this:

[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所知的正确名称约定.

My test class, JsonReaderTest.class, is placed in src/test/java and follows the correct name convention as far as I know from maven-surefire-plugin.

在Maven之外运行时,测试运行良好.

Tests run fine when run outside of Maven.

我的pom中包含此插件:

I have this plugin included in my 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();
    }
}

当我尝试谷歌搜索此问题时,似乎唯一可能出错的是命名约定(类必须以测试结尾或以测试开头,我对这两者均未做任何更改地进行了测试),并且应该将测试类放入适当的文件夹.

When I tried googling this problem, it seemed the only thing that could be wrong would be naming convention (class had to end with or start with test, I tested both with no change) and that the test class should be put into the appropriate folder.

当我运行时: 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也可以在目标/测试类内部正确生成

The JsonReaderTest.class is also correctly generated inside target/test-classes

这里的罪魁祸首是什么?

What could be the culprit here?

推荐答案

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

Using the Maven Surefire plugin and JUnit 5 together requires some tweaking ...

来自文档:

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

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.

由于Surefire 2.20中发生内存泄漏,因此junit-platform-surefire-provider目前仅适用于Surefire 2.19.1.

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

这篇关于测试未通过Maven运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆