Dumpcats
Dumpcats

Reputation: 453

JUnit Ant ClassNotFoundException

I am getting a java.lang.ClassNotFoundException when I run my target TranslatorWorkflow that is supposed to execute a JUnit test. I am running a build.xml file with the targets: build TranslatorWorkflow. It compiles but fails on the JUnit test immediately. My TranslatorWorkflow.class file is in {basedir}/bin/testScripts/. My classpath and target are:

classpath:

<path id="classpath">
    <fileset dir="${basedir}/lib/binaries" includes="*.jar" />
    <pathelement location="${basedir}/bin/testScripts/" />
</path>

TranslatorWorkflow target in my build.xml file:

<target name="TranslatorWorkflow">
    <mkdir dir="${junit.output.dir}" />
    <junit fork="yes" printsummary="withOutAndErr">
        <formatter type="xml" />
        <test name="testScripts.TranslatorWorkflow" todir="${junit.output.dir}" />
        <classpath refid="classpath" />
    </junit>
</target>

I attempted to emulate this answer to a similar question by adding the pathelement line shown in my classpath section above, but received the same exception. I've looked at this question as well as it seems like the same deal. I'd imagine there is something super obvious that I'm missing but alas I don't seem to be getting it.

Upvotes: 1

Views: 671

Answers (2)

Victor
Victor

Reputation: 3978

Dumpcats, try this...

  <path id="base.path">
    <pathelement path="${sun.boot.class.path}"/>
    <pathelement path="${sun.boot.library.path}"/>
    <fileset dir="${basedir}"> 
       <include name="**.jar"/>
    </fileset>
  </path>

And then in the target element ...

<target name="runTest">
  <mkdir dir="test_reports"/>

  <junit
      fork="true"
      forkmode="once"
      maxmemory="256m">

    <formatter type="plain" usefile="false"/>
    <formatter type="xml"/>

    <classpath refid="base.path"/>

    <batchtest
        haltonerror="true" haltonfailure="true"
        todir="test_reports">

      <fileset dir="${test.build}">
        <include name="**/*Test*.class"/>
        <include name="**/Test*.class"/>
        <include name="**/*Test.classp"/>
        <!-- Exclusions -->
        <exclude name="**/util/*.class"/> 
      </fileset>
    </batchtest>
  </junit>
</target>

At least is that is how i manage classpath reference and everything works like a charm.

Upvotes: 0

M A
M A

Reputation: 72854

The classpath should reference ${basedir}/bin not ${basedir}/bin/testScripts (i.e. it should reference the root of classes directory, not the package in which the class exists):

<path id="classpath">
   <fileset dir="${basedir}/lib/binaries" includes="*.jar" />
   <pathelement location="${basedir}/bin/" />
</path>

Upvotes: 1

Related Questions