1
votes

Background

I am trying to create an Ant script that:

  • compiles source files
  • compiles junit tests
  • runs junit tests of a certain category

Most of what I have done so far, have been taken from this question.

This is what I have in my ant script so far:

<?xml version="1.0"?>
<project name="junit_tests">
  <property name="build-main" location="build/main"/>
  <property name="build-test" location="build/main"/>
  <property name="src-test" value="java/test" />
  <property name="src-com" value="java/com" />
  <property name="src-lib" value="webapp/WEB-INF/lib" />
  <property name="test-lib" value="java/test/lib" />
  <path id="classpath">
    <fileset dir="${src-lib}" includes="*.jar"/>
    <fileset dir="${test-lib}" includes="*.jar" />
  </path>
  <path id="classpath.test">
    <path refid="classpath"/>
    <pathelement location="${build}"/>
  </path>
  <target name="init">
    <mkdir dir="${build-main}" />
    <mkdir dir="${build-test}" />
  </target>

  <target name="compileSrc" depends="init" description="compile src files ">
    <javac debug="on" destdir="${build-main}" encoding="UTF-8" fork="true" memorymaximumsize="1024m" includeantruntime="true">
      <src path="${src-com}" />
      <classpath refid="classpath"/>
    </javac>
    <javac debug="on" destdir="${build-main}" encoding="UTF-8" fork="true" memorymaximumsize="1024m" includeantruntime="true">
      <src path="${src-test}" />
      <classpath refid="classpath"/>
    </javac>
  </target>


  <!-- Test and build all files  -->
  <!-- To run this: use "ant" (default) or "ant run" -->
  <target name="run" depends="compileSrc">
    <junit printsummary="on" haltonfailure="no" fork="true">
        <classpath>
          <path refid="classpath.test" />
          <pathelement location="${build-test}"/>
        </classpath>
        <formatter type="xml" />
        <batchtest>
            <fileset dir="${build-main}" includes="**/NonDBTest.class" />
        </batchtest>
    </junit>
  </target>
</project>

Output from junit task

run: [junit] WARNING: multiple versions of ant detected in path for junit [junit] jar:file:/usr/local/Cellar/ant/1.9.7/libexec/lib/ant.jar!/org/apache/tools/ant/Project.class [junit] and jar:file:/Users/arnab/work/my_project/webapp/WEB-INF/lib/ant.jar!/org/apache/tools/ant/Project.class [junit] Running test.NonDBTest [junit] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.178 sec

BUILD SUCCESSFUL Total time: 37 seconds

Problem The test class that is in this category NonDBTest has a total of 7 tests, so junit is clearly not running all of them.

NonDBTest.java is just a interface with no methods, and this is what my test class looks like

@Category(NonDBTest.class)
public class MyTestClass extends AbstractTestCase{
....

@Test
public void testA(){ ... }

@Test
public void testB(){ ... }

@Test
public void testC(){ ... }

@Test
public void testD(){ ... }

@Test
public void testE(){ ... }

}

Question What am I doing wrong? I have tried changing NonTestDB to a class. I've tried implementing the interface but nothing has worked so far. How can I get junit to run all classes that are in the NonTestDB category? Am I missing something?

1
I've added an answer to your linked question which makes it possible to run tests of a specific Category via ant.R. Oosterholt

1 Answers

0
votes

Using JUnit categories is easier with maven, since you then can configure the categories to run via the group attribute.

I doubt there is explicit support for categories in ant, and I don't think you can include a JUnit Category interface in the test task (as you seem to do).

The work around is to also create a JUnit Suite for your category, and then to only run that Suite from the ant target:

@RunWith(Categories.class)
@IncludeCategory(NonDBTest.class)
@SuiteClasses( { MyTestClass1.class, MyTestClass2.class })
public class NonDBTestSuite {
  // will run only tests annotated with NonDBTest from
  // MyTestClass1 and MyTestClass2.
}

Then your ant configuration should indicate that the NonDBTestSuite should be executed.

<fileset dir="${build-main}" includes="**/NonDBTestSuite.class" />

Note that the answer you are referring to also has this Suite defined.

The not so nice part of this is that you need to maintain all the suite classes. What I did with multiple categories is maintain one AllTests suite with all test classes. Smaller suites would then use this AllTests in the SuiteClasses annotation.