0

我在 Eclipse 的项目中使用“JUnit 测试用例”新文件对话框向测试目录(与我的 src 目录平行)添加了一些单元测试。我有两个构建器,默认的 Java 构建器和我添加的 AntBuilder。Java Builder 继续工作,但 AntBuilder 在 Eclipse 中失败。当我选择 Project -> Build All 时,它会显示:

Buildfile: C:\source\machine-paint\eclipse\machine-paint\src\build.xml

clean:
      [delete] Deleting directory C:\source\machine-paint\eclipse\machine-paint\build

compile:
       [mkdir] Created dir: C:\source\machine-paint\eclipse\machine-paint\build\classes
       [javac] Compiling 33 source files to C:\source\machine-paint\eclipse\machine-paint\build\classes
       [javac] C:\source\machine-paint\eclipse\machine-paint\test\stencil\BorderWalkerTest.java:3: error: package org.junit does not exist
       [javac] import static org.junit.Assert.*;
       [javac]                        ^

然后在一些类似的错误之后,这个:

 [javac] C:\source\machine-paint\eclipse\machine-paint\test\stencil\BorderWalkerTest.java:8: error: incompatible types
       [javac]  @Test
       [javac]   ^
       [javac]   required: Annotation
       [javac]   found:    Test

这是我的构建文件。在这一点上,我真的在黑暗中开枪,例如将那个类路径标记添加到 javac 命令。

<?xml version="1.0" encoding="UTF-8"?>
<project name="Builder" default="jar" basedir=".">
  <target name="clean">
    <delete dir="build"/>
  </target>

  <target name="compile" depends="clean">
    <mkdir dir="build/classes" />
    <javac srcdir="." destdir="build/classes" debug="true" includeantruntime="true">
      <classpath>
        <path id="org.junit" location="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar" />
      </classpath>
    </javac>
  </target>

  <target name="jar" depends="compile">
    <jar destfile="../../stencil.jar" basedir="build/classes">
      <manifest>
        <attribute name="Main-Class" value="stencil.Main" />
      </manifest>
    </jar>
    <copy file="../../stencil.jar" tofile="../../../robotsdoart/stencils/stencil.jar" />
  </target>

  <target name="run">
    <java jar="build/jar/stencil.jar" fork="true" />
  </target>
</project>
4

3 回答 3

2

尝试将 srcdir 更改为您的实际源目录,使其不包含测试。

就像是:

<javac srcdir="src/" destdir="build/classes" debug="true" includeantruntime="true">
  <classpath>
    <path id="org.junit" location="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar" /> <!-- not sure if you need this -->
  </classpath>
</javac>
于 2012-10-11T21:46:55.243 回答
0

尝试junit直接包含您的 jar 文件。还要确保 JAR 文件在该位置可用。

<javac 
   srcdir="." 
   destdir="build/classes" 
   debug="true" 
   includeantruntime="true"
   classpath="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar"
 />

或将path元素定义为:

 <path id="lib.path.id">
    <fileset dir="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib" 
             includes="junit.jar"/>
 </path>

javac并在as中使用相同的

<javac 
   srcdir="." 
   destdir="build/classes" 
   debug="true" 
   includeantruntime="true"
   classpathref="lib.path.id""
 />

希望这可以帮助。

于 2012-10-11T21:41:44.467 回答
0

创建一个路径元素并在 javac 中引用该 id 有所帮助。

<path id="lib.path.id.junit.jar">
    <fileset dir="${project-path}\lib" includes="*.jar" />
</path>

然后我使用 classpathref 在 javac 中引用它

<target name="compile" depends="copy" description="compile all the source files in both src and test directories and should store all the class files in the bin directory.">
    <javac destdir="bin" includes="*.java" classpathref="lib.path.id.junit.jar" includeantruntime="true" debug="true" >
        <src path="src" /> 
        <src path="test" />
    </javac>

</target>
于 2020-06-23T11:59:15.967 回答