Project and test cases classes should be placed in separate directories in your project main directory, for example build/classes and build/tests.In next step you should prepare block.xml file which has classloader with classpath entry pointing at build/classes directory.

<container name="my-container>

  <classloader>
    <classpath>
      <fileset dir="build/classes"/>
    </classpath>
  </classloader>

</container>

All required libraries should be placed on JUnit classpath, below you can find sample ant target.

<junit showoutput="true" fork="true" printsummary="yes" newenvironment="true">
  <sysproperty key="basedir" value="${basedir}"/>
  <classpath>
    <pathelement path="${build.tests.dir}"/>
    <pathelement path="${build.classes.dir}"/>
    <path path="${libs.dir}/merlin-kernel-unit-3.2.5.jar"/>
    <path path="${libs.dir}/avalon-repository-main-1.2.jar"/>
    <path path="${libs.dir}/avalon-framework-api-4.1.5.jar"/>
    <path path="${libs.dir}/mrt-service-impl-0.1.jar"/>
    <path path="${libs.dir}/hibernate-2.1.2.jar"/>
  </classpath>
  <batchtest fork="true" todir="${build.tests.dir}">
    <formatter type="xml"/>
    <fileset dir="${build.tests.dir}">
      <include name="com/mrt/vend/impl/tests/**"/> 
    </fileset>
  </batchtest>
</junit>

It's very important to run tests in separate VM (fork="true"), because only then we avoid use of ant's classloader.

  • No labels