[英]problem compiling a junit test class with ant
I am having issues integrating junit with a working ant build.xml file. 我在将junit与工作的ant build.xml文件集成时遇到问题。 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.
在我学习如何使用ant时,我只想编译所有源代码和测试类。
I am using eclipse and the junit test classes work fine when execute through eclipse. 我正在使用eclipse,junit测试类在通过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.
这意味着使用junit.jar和ant-junit-1.7.0.jar正确设置了类路径(至少从eclipse的角度来看),尽管我不确定后一个jar是否绝对必要。
My folder structure is: 我的文件夹结构是:
src/code/MyClass.java src/code/MyClassTest.java 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: 并且ant文件只包含一个目标,只是为了编译MyClass和MyClassTest,我目前不包含任何junit任务,并且不介意将构建文件放在同一目录中:
<target name="compile" >
<javac srcdir="src/" />
</target>
Ant worked fine until I added MyClassTest.java (Junit with annotations) in my folder. Ant工作正常,直到我在我的文件夹中添加了MyClassTest.java(带注释的Junit)。 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. 我的想法是,Ant无法找到junit库。 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?
由于我没有指定类路径,我假设Ant会查找与源文件相同的位置以查找它需要的内容...我怎么能告诉Ant junit jar就在那里?
Any ideas, are really appreciated. 任何想法,真的很感激。 Regards
问候
Yes, you do have to specify a CLASSPATH. 是的,您必须指定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. 自从我上次使用Eclipse以来已经很长时间了,但我相信您必须右键单击项目根目录,选择“属性”,然后将JUnit JAR添加到CLASSPATH。
Ant has to know the CLASSPATH when you use it to build. 当你使用它来构建时,Ant必须知道CLASSPATH。 Have a look at their <path> stuff.
看看他们的<path>东西。
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>
Jars have to be specified by name, not by directory. 必须通过名称而不是目录来指定Jars。 Use:
采用:
<javac srcdir="src/" classpath="pathtojar/junit.jar"/>
Where "pathtojar" is the path containing the junit jar. 其中“pathtojar”是包含junit jar的路径。
Actually I didn't have any luck until I used fetch.xml per apache website to pull jar dependencies. 实际上我没有任何运气,直到我使用每个apache网站的fetch.xml来拉取jar依赖。 Then it worked fine for me.
然后它对我来说很好。 This is what I did:
这就是我做的:
JUnit is built in with Eclipse but with ANT I tried several solutions from apache and ant documentation but this is what worked for me. JUnit是用Eclipse构建的,但是使用ANT我尝试了几种来自apache和ant文档的解决方案,但这对我有用。 (I am on Windows 7) -Erik
(我在Windows 7上)-Erik
You need to define a classpath and use it with the javac task. 您需要定义类路径并将其与javac任务一起使用。
See: http://ant.apache.org/manual/using.html for some basics. 有关基础知识,请参阅: http : //ant.apache.org/manual/using.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.