Hello Everyone! First of all thanks again for viewing my thread. I was hoping someone could give me some good advice or point me to some code examples of java code calling an Ant JUnit Target if that makes sense. Basically I have a very basic java gui that upon button click will call an Ant JUnit target so that the user can test various test cases. I was told that the Ant JUnit target could be called somehow using straight java. So far I have not been able to find anything of use. Thanks again for viewing my thread.


7 Years
Discussion Span
Last Post by Jolint

Why don't you call JUnit directly instead of trying to trigger it by Ant?

The reason is because the Gui built by Java code needs input from the user. So I was told that Java calls to Ant are somewhat easy to do and my JUnit testing project is built by ant so it seems to me logical to call Ant by the Java Gui to build the JUnit tests if that makes sense.


Thank you again for your reply. I have just been trying to get some pieces working. I have my Ant File which when called works fine. It goes through some JUnit tests and displays results.

Now I have been doing the basic setup from the link you sent me:

1. I have included all my three jars in the Eclipse Class Path and
created my java file.
2. In the java file I have included

File buildFile = new File("build.xml"); //I specified the exact
// location of xml file
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());//I have also tried
//using my exact targets from the Ant File

but I get a classnotfound error. I am now thinking it might be a classpath problem but I'm not sure? Thanks!



I know you must have resolve your issue, but would like to share the resolution which might be helpful for someone else.

I also followed the link, and get the same classnotfound error, to resolve this i just added "ant.jar" and "ant-nodeps.jar" in my classpath. You can find these jars from your Ant/lib folder.

Hope this is helpful.

Thanks & Regards,

<target name="test" depends="compile">
       <junit fork="yes" forkmode="once" haltonfailure="no" showoutput="no" printsummary="yes">
         <formatter type="xml"/>
            <pathelement location="${build.classes}"/>
            <path refid="test.cp"/>
         <batchtest todir="${build.reports}">
            <fileset dir="src">
               <include name="JunitExample.java"/>

Get more from http://www.javarmi.com/2010/09/junit-and-ant/

Edited by Jolint: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.