Java – Ant + JUnit: NoClassDefFoundError

antclasspathhamcrestjavajunit

Ok, I'm frustrated! I've hunted around for a good number of hours and am still stumped.

Environment: WinXP, Eclipse Galileo 3.5 (straight install – no extra plugins).

So, I have a simple JUnit test. It runs fine from it's internal Eclipse JUnit run configuration. This class has no dependencies on anything. To narrow this problem down as much as possible it simply contains:

@Test
public void testX() {
    assertEquals("1", new Integer(1).toString());
}

No sweat so far. Now I want to take the super advanced step of running this test case from within Ant (the final goal is to integrate with Hudson).

So, I create a build.xml:

<project name="Test" default="basic">
    <property name="default.target.dir" value="${basedir}/target" />
    <property name="test.report.dir" value="${default.target.dir}/test-reports" />

    <target name="basic">
        <mkdir dir="${test.report.dir}" />
        <junit fork="true" printSummary="true" showOutput="true">
            <formatter type="plain" />
            <classpath>
                <pathelement path="${basedir}/bin "/>
            </classpath>
            <batchtest fork="true" todir="${test.report.dir}" >
                <fileset dir="${basedir}/bin">
                    <include name="**/*Test.*" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

${basedir} is the Java project name in the workspace that contains the source, classes and build file. All .java's and the build.xml are in ${basedir}/src. The .class files are in ${basedir}/bin.

I have added eclipse-install-dir/plugins/org.junit4_4.5.0.v20090423/junit.jar to the Ant Runtime Classpath via Windows / Preferences / Ant / Runtime / Contributed Entries. ant-junit.jar is in Ant Home Entries.

So, what happens when I run this insanely complex target? My report file contains:

Testsuite: com.xyz.test.RussianTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

Testcase: initializationError took 0 sec
Caused an ERROR
org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

What is this org.hamcrest.SelfDescribing class? Something to do with mocks? OK, fine. But why the dependency? I'm not doing anything at all with it. This is literally a Java project with no dependencies other than JUnit.

Stumped (and frustrated)!!

Best Answer

I downloaded JUnit 4.7 and put junit-4.7.jar in my build path (instead of the older version). That solved it. I didn't touch ant.

Related Topic