0

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.
  • 1
    Are you using Ant 1.9.0, by chance? Take a look at [Bamboo Ant Task fails when running junit task](http://stackoverflow.com/a/16490171/1078068). – Chad Nouis May 10 '13 at 22:13
  • Thanks for referring this. I found out the answer myself, which is that the classpath task is not adding the new path to the classloader and the already running JVM can't find the specified jar. And yes, I am using Ant 1.9. If you'd like please extract your comment to an answer containing this information and I'll happily accept it. – Dariusz Jędrzejczyk May 11 '13 at 18:46

0 Answers0