アプリケーション コードからテスト クラスにアクセスするのではなく、メイン (同じメイン) をテスト スコープに作成し、プロジェクトの追加のアーティファクトを作成する必要があります。
ただし、この追加のアーティファクト (jar) には、次のものが必要です。
- テストクラス
- アプリケーション コード クラス
- アプリケーション コードに必要な外部依存関係 (
compile
範囲内)
- テスト コードに必要な外部依存関係 (
test
範囲内)
これは基本的に、テスト クラス (およびその依存関係) が追加されたファット jar を意味します。Maven Jar プラグインとその目的test-jar
は、このニーズに合わないでしょう。Maven Shade プラグインとそのオプションshadeTestJar
はどちらにも役に立ちません。
では、Maven でテスト クラスと外部依存関係を含むファット jar を作成するにはどうすればよいでしょうか。
この場合、 Maven Assembly Pluginが最適な候補です。
最小限の POM サンプルを次に示します。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.sample.TestMain</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
上記の構成は、テスト クラスで定義したメイン クラスを設定しています。しかし、それだけでは十分ではありません。
また、記述子ファイルを作成する必要があります。src\main\assembly
フォルダにassembly.xml
は、次の内容のファイルがあります。
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
上記の構成は次のとおりです。
- 外部依存関係をスコープから取得するように設定し
test
ます (スコープも取得しcompile
ます) 。
fileset
パッケージ化されたファット jar の一部としてコンパイル済みのテスト クラスを含めるようにa を設定する
- 分類子を使用して最終的な jar を設定し
fat-tests
ます (したがって、最終的なファイルは のようになりますsampleproject-1.0-SNAPSHOT-fat-tests.jar
)。
target
その後、(フォルダーから) 次のようにメインを呼び出すことができます。
java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar
このようなメインから、次のようにすべてのテスト ケースを呼び出すこともできます。
- JUni テスト スイートを作成する
- 関連するテストをテスト スイートに追加します。
- プレーンな Java メインからテスト スイートを呼び出す
テスト スイートの例:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AppTest.class })
public class AllTests {
}
注: この場合、テスト スイートはAppTest
サンプル テストのみに関するものです。
次に、次のようなメインクラスを作成できます。
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
public class MainAppTest {
public static void main(String[] args) {
System.out.println("Running tests!");
JUnitCore engine = new JUnitCore();
engine.addListener(new TextListener(System.out)); // required to print reports
engine.run(AllTests.class);
}
}
上記のメインは、構成されたすべてのテストをチェーンで実行するテストスイートを実行します。