【问题标题】:Junit tests wont run using antJunit 测试不会使用 ant 运行
【发布时间】:2016-03-01 14:38:26
【问题描述】:

我已经能够使用 ant 成功编译和运行这个项目。但是当我尝试运行我的测试文件时,它给了我一个奇怪的 Reference ./lib/junit-4.11.jar not found。我可能在 junit 任务中必须提到的 refid 中做错了什么。请指出错误。

<target name="clean">
    <delete dir="build"/>
</target>

<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="./src/com/twu/biblioteca/" destdir="build/classes"/>
</target>

<target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/Application.jar" basedir="build/classes">
        <manifest>
            <attribute name="Main-Class" value="com.twu.biblioteca.Application"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <java jar="build/jar/Application.jar" fork="true"/>
</target>

<target name="junit" depends="compile">

    <junit printsummary="yes" haltonfailure="no">

        <!-- Project classpath, must include junit.jar -->
        <classpath refid="./lib/junit-4.11.jar" />

        <!-- test class -->
        <classpath location="./test/BooksController" />

        <test name="com.twu.biblioteca.BooksControllerTest"
              haltonfailure="no" todir="./report">
            <formatter type="plain" />
            <formatter type="xml" />
        </test>

    </junit>
</target>

【问题讨论】:

    标签: java ant junit


    【解决方案1】:

    问题在于您的 junit classpath 元素。 refid 属性允许您引用预定义的引用,而 ant 会通知您没有这样的引用。此外,您应该在 junit 任务下只有一个 classpath 元素(它可能与多个一起工作,但文档只支持一个)。

    删除那些 classpath 元素并将它们替换为

    <classpath>
        <pathelement location="lib/junit-4.11.jar"/>
        <pathelement location="test/BooksController"/>
    </classpath>
    

    它应该可以工作。在这里,我们通过将 junit jar 添加到路径然后包含测试的目录来构建类路径。您可能还必须将您的主类添加到其中,另一个 pathelement 给出它们的位置。

    有关这些类路径结构的更多信息,请参阅the ant documentation

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2012-11-09
      • 2011-12-05
      相关资源
      最近更新 更多