Answer the question
In order to leave comments, you need to log in
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).
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
Did it anyway...
<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>
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>
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 questionAsk a Question
731 491 924 answers to any question