java - Ant 包 org.junit 不存在

标签 java junit ant

我第一次使用 Ant,任务设置为项目将创建 jar 文件、运行测试、运行 jar 文件。通过使用此资源"Getting started – Ant",除了由于我不清楚的原因进行的测试之外,一切都已成功获得。我尝试下载测试,但没有成功,因为由于某种原因,junit 库不可见。以下是我的错误:

[mkdir] Created dir: D:\Eclipse project\CalcSimple\build\main
[javac] Compiling 3 source files to D:\Eclipse project\CalcSimple\build\main
[javac] D:\Eclipse project\CalcSimple\src\testing\CalcAPITest.java:4: error: package org.junit does not exist
[javac] import org.junit.Test;

enter image description here

测试本身在计算一个原始计算器。 无法帮助找到错误并建议如何修复它? 项目结构: enter image description here

我的代码:

<project name="CalcAnt" basedir="." default="main">


    <property name="build.dir"   value="build"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>
    <property name="main.build.dir" value="build/main"/>
    <property name="main.src.dir" value="src"/>
    <property name="test.build.dir" value="build/test"/>
    <property name="test.src.dir" value="src/testing"/>
    <property name="main-class"  value="CalcSimples.CalcSimple"/>

    <!--PATH LIB-->
    <path id="classpath.test">
        <pathelement location="lib/junit-4.11.jar"/>
        <pathelement location="lib/hamcrest-core-1.3.jar"/>
        <pathelement location="${main.build.dir}"/>
    </path>

        <!--CLEAN-->
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

        <!--COMPILE-->
    <target name="compile">
        <mkdir dir="${main.build.dir}"/>
        <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false"/>
    </target>

        <!--TEST-COMPILE-->
    <target name="test-compile" depends="compile">
        <mkdir dir="${test.build.dir}"/>
        <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
            <classpath refid="classpath.test"/>
        </javac>
    </target>

    <!--TEST-->
    <target name="test" depends="test-compile">
        <junit printsummary="on" haltonfailure="yes" fork="true">
            <classpath>
                <path refid="classpath.test"/>
                <pathelement location="${test.build.dir}"/>
            </classpath>
            <formatter type="brief" usefile="false" />
            <batchtest>
                <fileset dir="${test.src.dir}" includes="**/*Test.java" >
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!--CREATE JAR-->
    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${main.build.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <!--RUN JAR-->
    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <!--CLEAN-BUILD-->
    <target name="clean-build" depends="clean,jar"/>

    <!--MAIN-->
    <target name="main" depends="clean,run"/>
</project>

最佳答案

您的测试位于 src 下方,并且您的“编译”目标尝试编译它,但由于 javac 任务不了解 junit.jar,因此失败。

您需要将测试移出 src 目录,或者明确排除测试由“compile”目标编译。如果您想将 testing 保留为 src 的子目录,请将“compile”中的 javac 更改为

<javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false">
    <exclude name="testing/**"/>
</javac>

不过,将主要源代码和测试源代码保存在单独的源代码树中被认为是最佳实践。通常您会创建类似 src/main 的内容,将 CalcSimples 目录移动到那里,然后将 main.src.dir 设置为 src/main.

关于java - Ant 包 org.junit 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47439370/

相关文章:

java - Selenium junit.framework.AssertionFailedError :No Tests Found

java - Spring、Tomcat 6 和 ANT 构建脚本 : java. lang.ClassNotFoundException : org. springframework.web.context.ContextLoaderListener

java - Ant 自定义条件typedef:确保在编译期间可以使用编译的类(鸡肉和鸡蛋)

Java、MySQL、Spring 应用程序

java - SpringBoot Junit bean Autowiring

java - Play 2.0 并未运行所有测试

apache-flex - Ant 任务 compc for air 错误

Java 2D "tank"游戏 : Shooting 'missile' from rotated sprite doesn't work

java - 并发问题在 Google Maps Widget 上绘制 google maps GeoCoder 请求

java - 关闭阅读器后是否需要关闭 InputStream