Executing JUnit tests in Ant build.xml - java.lang.NoClassDefFoundError

73 views Asked by At

I am trying to run my JUnit tests using Ant build.xml but I am getting a java.lang.NoClassDefFoundError, how should I configure my build.xml file to make it work?

My Project Structure In Eclipse

apps/deploy/APP-INF/classes/test -> the compiled classes in are this path of my project folder

<target name="JUnitTest">
  <junit>
    <classpath>
      <pathelement path="apps/deploy/APP-INF/classes/test" />
      <pathelement location="apps/deploy/APP-INF/lib/junit.jar"/>
    </classpath>    
    <batchtest>
       <fileset dir="apps/deploy/APP-INF/src/test">
            <include name="**/*.java" />
       </fileset>
    </batchtest>
    <formatter type="brief" usefile="false"/>
  </junit>
</target>   

Console Log

My error is shown as above. Kindly appreciate any help!

2

There are 2 answers

0
Chew Kah Meng On BEST ANSWER

Solved: The issue was having the wrong directory in fileset and name. I changed it to this:

<batchtest>
    <fileset dir="${src.dir}/APP-INF/src">
        <include name="test/*.java"/>
    </fileset>
</batchtest>
0
Steephen On

Please specify your test.dir as shown below and refer it properly in your test target

<path id="classpath">
    <pathelement path="${java.class.path}"/>
    <fileset dir="${lib.dir}" includes="*.jar"/>
    <fileset dir="${build.dir}" includes="**/*.class"/>
    <fileset dir="${test.dir}" includes="**/*.class"/>
</path>

---   
<target name="test" depends="compile">
    <junit printsummary="on" haltonfailure="yes">
        <classpath refid="classpath"/>
        <formatter type="plain"/>
        <batchtest todir="${build.dir}">
            <fileset dir="${test.dir}" includes="**/*Test*.java"/>
        </batchtest>
    </junit>
</target>