A
A
Alexiuscrow2014-03-11 02:51:51
Java
Alexiuscrow, 2014-03-11 02:51:51

How to test java classes from under ant?

It is not possible to test java classes from under ant.
In addition to the default folders, I created 2 more folders in the root of the project:
1st - "classes" (here I throw off the results of compiling classes);
2nd - "test" (contains the same package (package name/address) as in src, but with a test class).
75e961d25590.png
Tell me, how to correctly describe the target for the test in "build.xml"?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexiuscrow, 2014-03-12
@Alexiuscrow

Did it anyway...

  1. I will transfer the folder with the test to src, to the "com.mntu.java.lab1.test" package (not important).
    Thus, along with compiling my test sources, I also compile the source with the test.
  2. in order for javac not to swear at methods unknown to it in the test, I added a classpath to javac in ant with the prescribed path to the junit-4.10.jar file (which I previously copied from External Libraries to the root of the project).
  3. and ant:
<property name="destclass" value="classes" description="Куда сохранить классы"/>
 
<target name="compile">
    <javac  srcdir="./src" destdir="${destclass}" classpath="./junit-4.10.jar"/>
</target>
 
<target name="jtest" depends="compile">
    <junit printsummary="on" showoutput="on">
        <test name="com.mntu.java.lab1.test.TestArtist"/>
        <classpath>
            <pathelement location="junit-4.10.jar"/>
            <pathelement location="${destclass}"/>
        </classpath>
    </junit>
</target>

Screenshot of the project file tree
4b1c6405ccc1.png

A
Alexey Kiselev, 2014-03-12
@alexeykiselev

To run tests, you need to pass two path structures to junit: the classpath and the compiled tests. The classpath (in the example, this is the ${test.classpath} variable) contains all the libraries and tested classes required to run the tests.
You need to pass the compiled tests to the dir parameter. Be careful not to put any helper classes from /test there as junit will try to execute them.
Example:

<target name="init-test" depends="prepare">
                <path id="test.classpath">
      <fileset dir="${lib}">
        <include name="*.jar" />
      </fileset>
      <pathelement location="${project.classes.build}" />
      <pathelement path="${test.build}" />
      <pathelement path="${test.build}/etc" />
      <pathelement path="${test.build}/etc/test" />
      <pathelement path="${test.build}/etc/mappings" />
    </path>
    <fileset id="test.fileset" dir="${test}">
      <include name="**/${test.class.pattern}.java"/>
      <exclude name="**/Abstract${test.class.pattern}.java"/>
      <exclude name="**/Base${test.class.pattern}.java"/>
      <exclude name="**/TestUtils.java"/>
    </fileset>
    <copy todir="${test.build}/etc/test">
      <fileset dir="${etc}/test"/>
    </copy>
    <copy todir="${test.build}/etc/test">
      <fileset dir="${etc}/mappings"/>
    </copy>
    <copy file="${etc}/hibernate.cfg.xml" todir="${test.build}/etc"/>
  </target>

  <target name="compile-test" depends="compile, run-checks">
    <javac srcdir="${test}"
      destdir="${test.build}"
      classpathref="run.classpath"
      source="${minimum.javaversion}"
      target="${minimum.javaversion}"
      debug="${debug.mode}"
      encoding="UTF-8"
      includeantruntime="no" />
    <copy todir="${test.build}">
      <fileset dir="${test}">
        <exclude name="**/*.java" />
      </fileset>
    </copy>
  </target>

  <target name="run-test" depends="init-test, compile-test">
    <mkdir dir="${test.xml}" />

    <junit 
      haltonfailure="off"
      haltonerror="off"
      errorproperty="test.failed"
      failureproperty="test.failed"
      showoutput="no"
      printsummary="yes"
      includeantruntime="yes"
      dir="${test.build}"
      fork="true">
      <jvmarg value="-Dfile.encoding=UTF8"/>
      <classpath>
        <path refid="test.classpath" />
      </classpath>
      <formatter type="xml"/>
      <batchtest todir="${test.xml}">
        <fileset refid="test.fileset" />
      </batchtest>
    </junit>
  </target>

  <target name="test" depends="run-test" description="Run unit tests">
    <fail if="test.failed"
      message="At least one test has failed. See logs (in ${test.xml}) for details (use the target test-report to run the test with a report)" />
  </target>

R
Ruslan Lopatin, 2014-03-11
@lorus

The classpath must contain the paths to the classes, not to the sources. So
<pathelement location="classes"/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question