Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ANT自动运行JUNIT案例,设置依赖项_Java_Testing_Ant_Junit - Fatal编程技术网

Java 使用ANT自动运行JUNIT案例,设置依赖项

Java 使用ANT自动运行JUNIT案例,设置依赖项,java,testing,ant,junit,Java,Testing,Ant,Junit,我的应用程序有以下设置: 项目TestAll当前包含一个java文件,该文件运行我所有其他项目中的所有TestAll javafile,并按预期工作。我面临的问题是,我希望这个TestAll.java从ant脚本运行,并让它将结果记录在报告文件中。这个javafile依赖于我的应用程序中的所有其他项目 这就是我到目前为止所做的: <?xml version="1.0"?> <project name="Ant-Test" default="main" basedir="."&

我的应用程序有以下设置:

项目TestAll
当前包含一个java文件,该文件运行我所有其他项目中的所有TestAll javafile,并按预期工作。我面临的问题是,我希望这个
TestAll.java
从ant脚本运行,并让它将结果记录在报告文件中。这个javafile依赖于我的应用程序中的所有其他项目

这就是我到目前为止所做的:

<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <property name="src.dir" location="" />
  <property name="build.dir" location="../bin" />
  <property name="dist.dir" location="../dist" />
  <property name="lib.dir" location="../lib" /> 
  <property name="test.dir" location="../src" />
  <property name="test.report.dir" location="../testreport" />


  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
  </target>

      <!-- Define the classpath which includes the junit.jar and the classes after compiling-->
      <path id="junit.class.path">
        <pathelement location="${lib.dir}/junit.jar" />
        <pathelement location="${build.dir}" />
      </path>

  <target name="makedir">
    <mkdir dir="${build.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}">
          <classpath refid="junit.class.path" />
        </javac>
  </target>

      <target name="junit" depends="compile">
        <junit printsummary="on" fork="true" haltonfailure="yes">
          <classpath refid="junit.class.path" />
          <formatter type="xml" />
          <batchtest todir="${test.report.dir}">
            <fileset dir="${src.dir}">
              <include name="**/*Test*.java" />
            </fileset>
          </batchtest>
        </junit>
      </target>

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

  <target name="main" depends="compile, jar, junit">
    <description>Main target</description>
  </target>

</project>
…依此类推,用于my
TestAll项目中的所有内部导入


这很可能是一个类路径错误,ANT无法找到它需要的文件,但我不知道如何解决它,并且已经尝试了几乎一整天。非常感谢任何帮助

提供给javac任务的类路径是:junit jar、构建目录和当前目录

除非当前目录(build.xml所在的目录)是
se
,否则javac任务将无法找到任何java文件来编译它们

鉴于此,javac任务的类路径将需要包括每个项目中每个
se
目录的路径

编辑:


注意:除非您计划将测试与其余代码打包在一起,否则您应该有两个javac任务,分别构建到一个构建目录和一个测试构建目录,然后提供到junit的每个任务的路径,这样junit就可以运行测试了,但只提供到jar任务的构建目录路径。

+1用于回答好的问题。
[javac] Compiling 1 source file to C:\Repositories\MyProject\TestAllProjects\bin
[javac] C:\Repositories\MyProject\TestAllProjects\src\se\testall\src\TestAllClass.java:6: error: package se.tlv.AProject.testall does not exist
[javac] import se.tlv.AnotherProject.testall.*;
[javac] ^
[javac] C:\Repositories\TestAllProjects\src\se\src\TestAllClass.java:7: error: package se.AnotherProject.testall does not exist