我选择 JUnit Ant任务解决方案,我通过任务元素提供 JUnit库:
<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">
我的问题是: