views:

981

answers:

3

Hello,

I am having issues integrating junit with a working ant build.xml file. My test class is in the same directory as my source classes. As I am learning how to use ant, I simply want to compile all the source and the test classes.

I am using eclipse and the junit test classes work fine when execute through eclipse. Which means the classpath is set up correctly (at least from eclipse's point of view) with junit.jar and ant-junit-1.7.0.jar, although I am not sure if the latter jar is absolutely necessary.

My folder structure is:

src/code/MyClass.java src/code/MyClassTest.java

and the ant file contain only one target, just to compile MyClass and MyClassTest, I do not include any junit tasks at the moment, and do not mind to have the build files in the same directory as well:

<target name="compile" >
  <javac srcdir="src/" />
</target>

Ant worked fine until I added MyClassTest.java (Junit with annotations) in my folder. The output is:

[javac] C:\....\src\MyClassTest.java:3: package org.junit does not exist
cannot find symbol

My thinking is that somehow Ant can't find the junit libraries. Since I do not specify a classpath, I was assuming that Ant would look at the same location as the source files to find to find what it needs...How can I tell Ant that the junit jars are right there?

Any ideas, are really appreciated. Regards

A: 

You need to define a classpath and use it with the javac task.

See: http://ant.apache.org/manual/using.html for some basics.

Nate
+3  A: 

Yes, you do have to specify a CLASSPATH.

It's been a long time since I last used Eclipse, but I believe you have to right click on the project root, choose "Properties", and add the JUnit JAR to the CLASSPATH.

Ant has to know the CLASSPATH when you use it to build. Have a look at their <path> stuff.

Here's how I do it:

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>
duffymo
+3  A: 

Jars have to be specified by name, not by directory. Use:

<javac srcdir="src/" classpath="pathtojar/junit.jar"/>

Where "pathtojar" is the path containing the junit jar.

Marco Mustapic
Thanks Marco and Duffy. Even though according to the eclipse package explorer view the junit.jar is referenced right under the root project name, I had to point to the jar directly via its absolute location.. I mean classpath="/junit.jar" just didn't work,I had to say classpath="C:\Users\Desktop\Work\Eclipse\plugins\org.junit4_4.3.1\junit.jar"Thanks again!
denchr