LoginSignup
0
0

More than 5 years have passed since last update.

maven+djunit 複数プロジェクトでのテスト依存関係設定

Last updated at Posted at 2016-02-15

概要

eclipse+m2eでは問題なかったのに、mavenコマンドライン+djunit実行時に起きる、以下の問題についの対応方法です。

  • mavenコマンドラインでテスト実行を行った場合、コンパイルエラーが起きる問題
  • VirtualMockObjectが一部有効にならない問題

解消方法

  • テスト依存関係を設定する
  • djUnit実行時にVirtualMockObjectを使用している場合、対象ソースルートを追加する。

コンパイルエラー

原因

テスト依存関係がeclipse上とmavenコマンドラインでは違うため

eclipse+m2e+djunitの参照関係

eclipse+m2e+junitの場合は、実行時にjava.lang.NoClassDefFoundErrorが起きる。
01_eclipse_m2e.png

mavenコマンドライン実行時の参照関係

ProjectB test から Project A testが参照されている場合コンパイルエラーとなる。
maven_command_line_default.png

解消方法

テスト依存関係を追加する

maven_command_line_add_settings.png

具体的な設定について

test-jarをコンパイルするように設定を追加する。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

test-jarの依存関係を追加する。

    <dependency>
      <groupId>groupId</groupId>
      <artifactId>artifactId</artifactId>
      <type>test-jar</type>
      <version>version</version>
      <scope>test</scope>
    </dependency>

djunit VirutalMockObjectが有効にならない

原因

jp.co.dgic.eclipse.project.source.dirで指定しているパスにVirtualMockObjectを適用するクラスファイルのソースがないとだめ。

解消方法

  • ダミーのフォルダを設定し、ダミーのファイルを用意する(大規模になると面倒)
  • 対象フォルダを追加する。
    • 「-Djp.co.dgic.eclipse.project.source.dir」に「;」区切りでディレクトリを追加する
    • 例:「-Djp.co.dgic.eclipse.project.source.dir="\${project.build.sourceDirectory};${project.basedir}\..\simple.pattern3.projecta\src\main\java"」

サンプル

以下のサイトでソースを公開しています。

参考文献

以下の資料を参考としました。感謝です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0