Maven无法运行测试?

25
当我在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结尾或开头,我测试了这两种情况都没有改变),并且测试类应该放在适当的文件夹中。
当我运行: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 中正确生成。
这里可能的罪魁祸首是什么?

1
你试图使用哪个版本的Junit?从你的测试类来看,我认为你想要使用Junit 5,但从你的Maven配置中看似乎是为了Junit 4。 - skadya
1
你的测试类中没有看到'package'行?你的测试类在默认包中吗? - P.An
1
我尝试重现你的例子,但是出现了以下错误: initializationError(package.maven.MyTest): The class package.maven.MyTest is not public.你能否尝试在你的类中添加public修饰符? - SilverNak
@SilverNak 对我有用!没有错误提示,但你提到他的类不是公共的。我看了一下,我的类也都不是公共的。而且Eclipse是为我创建这些类的!他们竟敢这样做!:) 谢谢。 - Marvo
7个回答

27

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

来自文档

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

3
从以上的文档链接可以得知,现在不再推荐使用junit-platform-surefire-provider。这个提供程序最初是由JUnit团队开发的,在JUnit Platform 1.3版本中被弃用,在1.4版本中则停止了支持。建议改用Maven Surefire本身提供的支持。 - Harry King

17
这个插件对我很有用:
            <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


7

以下的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 就足够了。 - Ahmed Nabil
这个解决方案除了测试范围依赖项之外,修复了我的问题。 - notacorn

4
我遇到了同样的问题。所有的测试类都是包私有的,JUnit 5 无法看到它们。解决方法是将以下依赖项添加到pom文件中:
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>

0

除了所有的JUnit依赖项,如引擎、API和启动器,这个集成应该包括所有的吧。

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.9.0-M1</version>
<scope>test</scope>

使用最新版本的Surefire插件
对我而言,它可以独立工作,无需其他JUnit依赖项


0

我曾经遇到过同样的问题,花了好几天才找到解决方案! 首先,请确保你的类名以***Test.java结尾,例如:MyGreatTest.java 其次,请将你的类和方法设置为public!这非常重要,否则mvn test会忽略你的测试类! 第三,在pom.xml中添加以下代码:

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

1
在JUnit 4和JUnit 5之间有所改变:测试类、测试方法和生命周期方法不再需要是public,但它们不能是private。因此,您可以省略'public'修饰符,这样它们就使用默认可见性(包本地)。 - GeertPt
1
不是这样的。如果您没有指定public或private,它被视为包本地,并且JUnit 5仍将找到您的测试。请参见:https://github.com/junit-team/junit5-samples/blob/r5.5.1/junit5-jupiter-starter-maven/src/test/java/com/example/project/CalculatorTests.java - GeertPt

0

对我来说:

maven-surefire-plugin 2.19 -> 测试未运行

maven-surefire-plugin 2.22.2 -> 测试未运行

maven-surefire-plugin 3.1.0 -> 正常

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>   
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.0</version>
</plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接