2017-11-07 19 views
5

Mavenプロジェクトでは、JUnit 4に依存している既存のテストがいくつかあります。これらのテストはJUnit 5で複数の理由で移行できません。
基本的に、JUnit 4ランナーを使用するライブラリに依存するテストがあり、コードの移行に時間がかかることがあります。
同じビルドでJUnit 4とJUnit 5のテストを実行

JUnit 5で新しいテストクラスを作成して、今すぐリリースされ、新しい興味深い機能を提供したいと思います。
どうすればいいですか?

答えて

9

JUnit5はa way out of the boxを提供します。

JUnitの5 = JUnitのプラットフォーム+ JUnitの木星+ JUnitのビンテージ

それぞれが別個のプロジェクトであり、それらのすべてを使用することのJUnit 4をコンパイルして実行することができ、同じプロジェクト内のJUnit 5試験。

JUnitの木星は5

JUnitのビンテージがベースのJUnit 3とJUnit 4を実行するためTestEngineを提供するJUnitでテスト、拡張機能を記述するための新しいプログラミングモデルと拡張モデルの組み合わせでありますプラットフォーム上のテスト

プラットフォームここJVM

にテストフレームワークを起動するための基盤として機能するJUnitはJUnit4とJUnit5試験の両方をコンパイルして実行するようにプロジェクトを構成するためのMavenで使用する最小構成である

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>mygroup</groupId> 
    <artifactId>minimal-conf-junit4-5</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <properties> 
     <!-- JUnit 5 depends on JDK 1.8 --> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
     <!-- JUnit dependency versions --> 
     <junit.version>4.12</junit.version> 
     <junit-vintage-engine>4.12.1</junit-vintage-engine> 
     <junit-jupiter.version>5.0.1</junit-jupiter.version> 
     <junit-platform.version>1.0.1</junit-platform.version> 
    </properties> 

    <dependencies> 
     <!--JUnit Jupiter API to write and compile tests with JUnit5 --> 
     <dependency> 
      <groupId>org.junit.jupiter</groupId> 
      <artifactId>junit-jupiter-api</artifactId> 
      <version>${junit-jupiter.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- JUnit 4 to make legacy JUnit 4 tests compile --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>${junit.version}</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.19.1</version> <!-- matters until now--> 
       <dependencies> 
        <!-- to let surefire to run JUnit 4 but also JUnit 5 tests --> 
        <dependency> 
         <groupId>org.junit.platform</groupId> 
         <artifactId>junit-platform-surefire-provider</artifactId> 
         <version>${junit-platform.version}</version> 
        </dependency> 
        <!-- JUnit vintage engine to run JUnit 3 or JUnit 4 tests --> 
        <dependency> 
         <groupId>org.junit.vintage</groupId> 
         <artifactId>junit-vintage-engine</artifactId> 
         <version>${junit-vintage-engine}</version> 
        </dependency> 
        <!-- JUnit 5 engine to run JUnit 5 tests --> 
        <dependency> 
         <groupId>org.junit.jupiter</groupId> 
         <artifactId>junit-jupiter-engine</artifactId> 
         <version>${junit-jupiter.version}</version> 
        </dependency> 
       </dependencies> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

mvn testは、JUnit 4テストとJUnit 5テストの両方をコンパイルして実行します。

注1:junit-vintage-engine4.12.1)とjunit4.12)の依存関係は、同じ正確なバージョンを指定しないでください。

  • それらの放出がそれら

  • junit-vintage-engine間で関連していない任意のJUnitのまたはテストを実行するように設計されていますよう
    これは全く問題ではありません。

注2:あなたは一人でJUnit5またはJUnit4とJUnit5の両方を使用したいものは何でも2.19.1バージョン事項とのmaven-の確実-プラグイン。
プラグインの次のバージョンでは、JUnit 5テストの実行中に実際にいくつかの例外が発生します。
The issueはかなり古いですが、まだ解決されていません。

+1

また、 'junit-jupiter-engine'が必要です。 –

+0

ありがとうMarc!私は確かに私のためにそれを追加したSpring Bootプロジェクトでテストしました。私は更新しました。 – davidxxx

関連する問題