簡體   English   中英

測試不通過 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

據我所知,我的測試類 JsonReaderTest.class 位於src/test/java中,並遵循正確的命名約定。

在 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 中正確生成

這里的罪魁禍首可能是什么?

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

這個插件對我有用:

            <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

以下 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 5 看不到它們。 解決方案是將此依賴項添加到 pom 文件中:

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

我遇到了同樣的問題,我花了幾天時間才找到解決方案! 首先,確保你的類名以***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依賴項,如引擎、api、啟動器,我猜這個集成了所有

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

使用最新版本的surefire插件
它完全適用於我,沒有其他的junite依賴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM