タスクの要素を介してJUnit ライブラリを提供するJUnit Ant タスクソリューションに行きました。
<target name="test" depends="compile-tests" description="Run unit tests">
<junit printsummary="true" haltonfailure="true">
<classpath refid="test.classpath" />
<test name="com.package.TestClass" />
</junit>
</target>
使用される重要なクラスパスは次のとおりです。
<path id="test.compile.classpath">
<path refid="compile.classpath" />
<path refid="test.lib.classpath" />
<pathelement location="${build.classes.dir}" />
</path>
<path id="test.classpath">
<path refid="test.compile.classpath" />
<pathelement path="${test.classes.dir}" />
</path>
そして、junit.jar で参照されているものは、Ant Maven タスクです。
<artifact:dependencies pathId="test.lib.classpath">
<dependency groupId="junit" artifactId="junit" version="4.11" />
</artifact:dependencies>
さて、問題は、このタスクを実行すると例外が発生することです。
java.lang.NoClassDefFoundError: junit/framework/TestListener
解決策は、タスクにfork="true"属性を追加することです。
<junit printsummary="true" haltonfailure="true" fork="true">
私の質問は次のとおりです。
- 新しい JVM インスタンスを実行しないと、JUnit Ant タスクがランタイムに junit.jar を含められないのはなぜですか?
- そして、このランタイムは実際には何ですか? Antスクリプトを実行しているものですか?jar をランタイムにアタッチすることは不可能ですか? 標準のクラスローディングだと思っていました。