Java 在Eclipse中使用Ant运行JUnit测试时出现问题。初学者问题

Java 在Eclipse中使用Ant运行JUnit测试时出现问题。初学者问题,java,eclipse,ant,junit,Java,Eclipse,Ant,Junit,这几天我在学习如何使用ant来运行自动化测试 我的项目的类路径中有JUnit。所有这些似乎都很好,我可以将其纳入我的课程: import junit.framework.TestCase; //line20 public class SimpleLattice1DTest extends TestCase{ ... } 我的build.xml是: <?xml version="1.0"?> <project name="Ant-Test" default="compile

这几天我在学习如何使用ant来运行自动化测试

我的项目的类路径中有JUnit。所有这些似乎都很好,我可以将其纳入我的课程:

import junit.framework.TestCase; //line20

public class SimpleLattice1DTest extends TestCase{
 ...
}
我的build.xml是:

<?xml version="1.0"?>
<project name="Ant-Test" default="compile" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir" location="." />
    <property name="build.dir" location="build" />
    <property name="dist.dir" location="dist" />
    <property name="docs.dir" location="docs" />
    <property name="test.dir" location="jlife/tests" />
    <property name="test.report.dir" location="test/report" />  

    <!-- Deletes the existing build, docs and dist directory-->
    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${docs.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  build, docs and dist directory-->
    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${docs.dir}" />
        <mkdir dir="${dist.dir}" />

        <mkdir dir="${test.report.dir}" />


    </target>

    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${build.dir}">
        </javac>

    </target>

    <!-- Creates Javadoc -->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <!-- Define which files / directory should get included, we include all -->
            <fileset dir="${src.dir}">
                <include name="**" />
            </fileset>
        </javadoc>
    </target>

    <!--Creates the deployable jar file  -->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\CoreTest.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Test" value="test.CoreTest" />
            </manifest>
        </jar>
    </target>


    <!-- Run the JUnit Tests -->
        <!-- Output is XML, could also be plain-->
        <target name="junit" depends="compile">
            <junit printsummary="on" fork="true" haltonfailure="yes">

                <formatter type="xml" />
                <batchtest todir="${test.report.dir}">
                    <fileset dir="${src.dir}">
                        <include name="**/*Test*.java" />
                    </fileset>
                </batchtest>
            </junit>
        </target>

</project>

当我在eclipse中运行它时,我得到以下错误:

[javac]C:\Documents和 设置\noname\Documenti\JLife\u git\JLife\u git\JLife\src\JLife\tests\SimpleRelatice1dtest.java:20: 包junit.framework不存在 [javac]导入junit.framework.TestCase


我想这有点问题,但我不知道。有人能告诉我正确的方向吗?

您的
javac
目标没有指定源目录和目标目录之外的任何内容-它没有添加任何类路径条目;您需要为相应的JUnitJAR文件添加一个条目。有关更多详细信息,请参阅。您可能希望将JUnit的路径指定为类路径属性、嵌套元素或对其他地方声明的路径的引用。

您的
javac
目标不指定源目录和目标目录之外的任何内容-它不添加任何类路径条目;您需要为相应的JUnitJAR文件添加一个条目。有关更多详细信息,请参阅。您可能希望将JUnit的路径指定为类路径属性、嵌套元素或对别处声明的路径的引用。

您需要指定包含.class文件和外部JAR(如JUnit)的目录

e、 g


这是我从中摘录的完整文件,以防您需要关于如何设置其他常见事物(emma、javadoc等)的想法


IMP的构建脚本

您需要指定包含.class文件和外部JAR(如junit)的目录

e、 g


这是我从中摘录的完整文件,以防您需要关于如何设置其他常见事物(emma、javadoc等)的想法


IMP的构建脚本

eclipse类路径与ant环境是分开的。在构建文件中,当调用
javac
时,需要提供一个classpath属性

您可以使用其他属性在文件顶部定义类路径,如下所示:

<path id="classpath">
    <fileset dir="[path to libraries]" includes="**/*.jar" />
</path>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />

eclipse类路径与ant环境是分开的。在构建文件中,当调用
javac
时,需要提供一个classpath属性

您可以使用其他属性在文件顶部定义类路径,如下所示:

<path id="classpath">
    <fileset dir="[path to libraries]" includes="**/*.jar" />
</path>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />

如果你观察错误堆栈,你会发现下面的一行,就在你提到的错误线上面

[javac][类文件的搜索路径:C:\Program files\Java\jre6\lib\resource

这一行显示了这个ant目标执行的类路径中可用的所有JAR。 您肯定不会在这里找到所需的jar,即junit-x.x.x.jar(junit-4.8.2.jar)

现在转到eclipse->Window->preferences->Ant->Runtime->Global Entries->AddJARAddJUnit-4.8.2jar(您将在项目库目录中找到)

如果您在错误堆栈中使用Ant->Runtime->classpath和与类路径相关的错误行,您将理解这个问题


希望这能解决您的问题。

如果您观察错误堆栈,您会发现下面一行,就在您提到的错误线上方

[javac][类文件的搜索路径:C:\Program files\Java\jre6\lib\resource

这一行显示了这个ant目标执行的类路径中可用的所有JAR。 您肯定不会在这里找到所需的jar,即junit-x.x.x.jar(junit-4.8.2.jar)

现在转到eclipse->Window->preferences->Ant->Runtime->Global Entries->AddJARAddJUnit-4.8.2jar(您将在项目库目录中找到)

如果您在错误堆栈中使用Ant->Runtime->classpath和与类路径相关的错误行,您将理解这个问题

希望这能解决你的问题