Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit run tests inside a JAR

I'm using pure JUnit 4 test and I have them compiled inside some jars that belongs to the classpath. I'd like to run all the tests in my classpath.

Is there a way?

My tasks looks like:

<target name="test" description="All the tests">
    <junit fork="yes">
        <classpath> 
            <path refid="test.classpath" />
            <fileset dir="war/WEB-INF/lib/">
                <include name="*.jar"/>
            </fileset>
            <fileset dir="war/WEB-INF"/>
            <fileset dir="war"/>
        </classpath>
    <formatter type="plain" usefile="false" />

    </junit>
</target>
like image 885
Jordi P.S. Avatar asked Sep 02 '12 19:09

Jordi P.S.


1 Answers

The example below assumes the JUnit tests are named with the suffix "Test" such as "MyClassTest.java" and that the JUnit 4 jar file is in a library directory pointed to by the property lib.dir. For each jar file containing compiled tests, a ZipFileSet may be used to select the test classes within a nested <batchtest> element. The path to the JUnit 4 jar is included in the classpath to ensure that the correct version is used.

<target name="test" description="All the tests.">
  <junit fork="true">
    <classpath>
      <fileset dir="war/WEB-INF/lib/" includes="**/*.jar" />
      <fileset dir="${lib.dir}" includes="junit*.jar" />
    </classpath>
    <batchtest haltonfailure="true">
      <zipfileset src="war/WEB-INF/lib/test.jar" includes="**/*Test.class" />
    </batchtest>
    <formatter type="plain" usefile="false" />
  </junit>
</target>
like image 172
Christopher Peisert Avatar answered Oct 05 '22 23:10

Christopher Peisert