141

JUnit 5 で簡単なテスト メソッドを作成しました。

public class SimlpeTest {
    @Test
    @DisplayName("Some description")
    void methodName() {
        // Testing logic for subject under test
    }
}

しかし、実行するmvn testと、次のようになりました。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running SimlpeTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

どういうわけか、surefire はそのテスト クラスを認識しませんでした。私pom.xmlのように見えます:

<properties>
    <java.version>1.8</java.version>
    <junit.version>5.0.0-SNAPSHOT</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-api</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>snapshots-repo</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <updatePolicy>always</updatePolicy>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

これを機能させる方法はありますか?

4

18 に答える 18

129

maven-surefire-plugin、現時点ではJUnit 5 を完全にはサポートしていませんSUREFIRE-1206でこのサポートを追加することに関して未解決の問題があります。

そのため、カスタム プロバイダを使用する必要があります。1 つはすでに JUnit チームによって開発されています。ユーザー ガイドから、junit-platform-surefire-providerプロバイダーとTestEngine新しい API の実装を追加する必要があります。

<build>
  <plugins>        
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <!-- latest version (2.20.1) does not work well with JUnit5 -->
      <version>2.19.1</version>
      <dependencies>
        <dependency>
          <groupId>org.junit.platform</groupId>
          <artifactId>junit-platform-surefire-provider</artifactId>
          <version>1.0.3</version>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>5.0.3</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

junit-jupiter-apiまた、必ず次のスコープで依存関係を宣言してtestください。

<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.3</version>
    <scope>test</scope>
  </dependency>
</dependencies>
于 2016-05-01T18:42:54.337 に答える
73

更新 2

問題は Maven Surefire プラグイン v2.22.0 で修正されました

新しいバージョンは Maven Central Repository で入手できます。

メイヴン

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
</dependency>

グラドル

compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0'

アップデート

マリアンが指摘したように、JUnit 5 プラットフォーム Surefire プロバイダー (1.2.0)の最新バージョンは、 Maven Surefire プラグイン (2.21.0)の最新バージョンをサポートします。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
            </dependencies>
        </plugin>



pom.xml

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

TestScenario.java

package test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class TestScenario {

    @Test
    @DisplayName("Test 2 + 2 = 4")
    public void test() {
        Assertions.assertEquals(4, 2 + 2);
    }
}

出力 (mvn クリーン インストール)

...
[情報] --- maven-surefire-plugin:2.21.0 :test (default-test) @ テスト --- [情報]
[情報] -------------- -----------------------------------------
[情報] テスト
[情報] -- -------------------------------------------------- ---
[INFO] 実行中の test.TestScenario
[INFO] テストの実行: 1、失敗: 0、エラー: 0、スキップ: 0、経過時間: 0.005 秒 - test.TestScenario
[INFO]
[INFO] 結果:
[ INFO] ]
[情報]テストの実行: 1、失敗: 0、エラー: 0、スキップ: 0
...


今日までの最も簡単な方法:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-surefire-provider</artifactId>
                <version>1.1.0</version>
            </dependency>
        </dependencies>
    </plugin>
于 2018-02-27T23:09:05.360 に答える
15

2019 年 8 月に、ここで尋ねたのと同じ問題に遭遇しました: Maven fails to find JUnit tests to run . これらの答えは私を正しい方向に導きましたが、問題をさらに簡潔に解決できることがわかりました。JUnit5 サンプル Maven プロジェクトからソリューションをコピーしました。

JUnit 5.5.1 および2.22.2 では、依存関係maven-surefire-pluginを追加する必要はありません。junit-platform-surefire-providerでこの 1 つの依存関係と 1 つのプラグインを指定するだけで十分pom.xmlです。

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>
于 2019-08-15T12:08:38.973 に答える
0

私のケースの経験に基づいて、pom.xmlのライブラリjunit-jupiter-engineに依存するべきではありません。次に、プラグイン maven-surefire-plugin と最新バージョンのjunit-jupiterへの依存関係を使用できます

于 2022-01-25T01:01:46.477 に答える