I went for the JUnit Ant task solution where I provide the JUnit library through the task's element:
<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>
The important classpaths used are:
<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>
And the referenced one with the junit.jar is an Ant Maven task:
<artifact:dependencies pathId="test.lib.classpath">
<dependency groupId="junit" artifactId="junit" version="4.11" />
</artifact:dependencies>
Now, the problem is that I get an exception when running this task:
java.lang.NoClassDefFoundError: junit/framework/TestListener
The solution is adding the fork="true" attribute to the task:
<junit printsummary="true" haltonfailure="true" fork="true">
My questions are:
- Why without running a new JVM instance does the JUnit Ant task fail to include the junit.jar in the runtime?
- And what is this runtime actually? Is it the one executing the Ant script? Is attaching a jar to the runtime impossible? I thought it would be standard class loading.