2017-09-22 6 views
3

私はこれを取得:テストは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 /テスト/ javaのに入れて、私の知る限りから知っているように、正しい名前の規則に従いれますmaven-surefire-pluginです。

Mavenの外部で実行したときにテストを正常に実行します。

<!-- 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(); 
    } 
} 

私はこれをグーグルで試してみました問題は、それが間違っている可能性がある唯一のものは、命名規則です(クラスはwiで終わらなければならない私は変更なしで両方をテストしました)、テストクラスを適切なフォルダに配置する必要があります。

私が実行します。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 docsから

+1

?あなたがテストクラスから、あなたはjunit 5を使いたいと思っていますが、あなたのmavenの設定はjunit 4のようです。 – skadya

+1

テストクラスで 'package'行が見えませんか?テストクラスはデフォルトのパッケージになっていますか? –

+1

あなたの例を作り直そうとしました。 'initializationError(package.maven.MyTest):クラスpackage.maven.MyTestはpublicではありません。 ' クラスにpublic修飾子を追加できませんか? – SilverNak

答えて

5

一緒にMavenプラグインシュアとJUnit 5を使用すると、いくつかの調整が必要で...

使用しようとしているのJUnitのバージョン

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.

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> 
... 
関連する問題