Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.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_Macos_Ant_Junit - Fatal编程技术网

Java Ant和带注释的junit测试

Java Ant和带注释的junit测试,java,macos,ant,junit,Java,Macos,Ant,Junit,我在./src/com.example.package.tests中有以下测试类 public class CardTest { @Test public void testCardCompareWithSameValue() throws Exception { Card card1 = new Card(Card.COLOR.CLUBS, 4); Card card2 = new Card(Card.COLOR.SPADES, 4);

我在./src/com.example.package.tests中有以下测试类

public class CardTest {
    @Test
    public void testCardCompareWithSameValue() throws Exception {
        Card card1 = new Card(Card.COLOR.CLUBS, 4);
        Card card2 = new Card(Card.COLOR.SPADES, 4);

        Card.MATCH_TYPE match = card1.compareCard(card2);

        assertEquals("The returned match must be of enum type Card.MATCH_TYPE.VALUE",
            Card.MATCH_TYPE.VALUE, match);
    }
}
我已经将junit-4.7.jar放在了./lib中

并具有以下内容。/build.xml

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

    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="com.example.package.gui.Main" />
    <property name="report.dir" value="${build.dir}/junitreport" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>


    <path id="application" location="${jar.dir}/${ant.project.name}.jar"/>


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

    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java" />
        </copy>
    </target>

    <target name="jar" depends="junit">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" excludes="**/*Test.class">
            <manifest>
                <attribute name="Main-Class" value="${main-class}" />
            </manifest>
        </jar>

    </target>


    <target name="junit" depends="compile">
        <mkdir dir="${report.dir}" />
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes">
            <classpath>
                <path refid="classpath" />
                <path location="${classes.dir}" />
                <path refid="application" />
            </classpath>

            <formatter type="xml" />

            <batchtest fork="yes">
                <fileset dir="${src.dir}" includes="*Test.java" />
            </batchtest>
        </junit>
    </target>

    <target name="junitreport" depends="junit">
        <junitreport todir="${report.dir}">
            <fileset dir="${report.dir}" includes="TEST-*.xml" />
            <report todir="${report.dir}" />
        </junitreport>
    </target>


    <target name="run" depends="jar">
        <java classname="${main-class}" fork="true" >
            <classpath>
                <path refid="classpath" />
                <path refid="application" />
            </classpath>
        </java>
    </target>

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

    <target name="main" depends="clean,run" />

</project>

为什么ant不运行我的junit测试?当我查看报告时,它说有0个测试

我正在使用OS X Yosemite,并通过二进制下载安装了Ant

编辑:同样使用ubuntu进行测试,没有任何更改。

根据,您必须使用
todir
属性告诉
batchtest
将测试报告放在哪里。否则,它会将它们写入当前工作目录

<batchtest fork="yes"  todir="${report.dir}">
            <fileset dir="${src.dir}" includes="*Test.java" />
        </batchtest>

应该有用

还可以检查当前工作目录,以查看是否有一组TEST-*.xml文件

根据您必须告诉
batchtest
使用
todir
属性将测试报告放置在哪里。否则,它会将它们写入当前工作目录

<batchtest fork="yes"  todir="${report.dir}">
            <fileset dir="${src.dir}" includes="*Test.java" />
        </batchtest>

应该有用

同时检查当前工作目录,查看是否有大量TEST-*.xml文件

当运行
时,您希望引用编译的TEST类文件,而不是测试Java源文件

我建议将常规源代码和测试源代码放在两个不同的目录中,然后将它们编译到两个不同的目录中。我们使用Maven目录标准:

  • src/main/java
    -常规源文件的位置
  • src/main/resources
    -资源的位置(放入jar的XML和JSON文件)
  • src/test/java
    -测试源文件的位置
  • src/test/resources
    -测试所需资源的位置
  • target/classes
    -编译常规源类文件的地方
  • 目标/测试类
    -编译测试类文件的地方
例如:

<target name="compile">
    <javac srcdir="src/main/java"
        destdir="target/classes">
        <classpath ref="main.classpath"/>
    </javac>
</target>

<target name="package"
    depends="compile">
    <jar destfile="target/${ant.project.name}.jar">
        <fileset dir="target/classes"/>
        <fileset dir="src/main/resources"/>
    </jar>
</target>

<target name="compile-test"
    depends="compile">

    <!-- Classpath must include the main.classpath from -->
    <!-- above, the test.classpath which will include   -->
    <!-- "junit.jar", and finally the compiled classes  -->
    <!-- from target/classes. Not 100% sure if this is  -->
    <!-- the correct way to specify it, but it's close. -->
    <javac srcdir="src/test/java"
        destdir="target/test-classes">
        <classpath>
             <path ref="main.classpath"/>
             <path ref="test.classpath"/>
             <path location="target/classes"/>
        <classpath>
    </javac>
</target>

<junit printsummary="yes"
    haltonfailure="yes"
    showoutput="yes">
    <classpath>
        <path refid="main.classpath" />
        <path refid="test.classpath" />
        <path location="${classes.dir}" />
    </classpath>

        <!-- Batchtest is pointing to the classes-->
        <formatter type="xml" />
        <batchtest fork="yes">
            <fileset dir="${target/test-classes}"/>
        </batchtest>
    </junit>
</target>

运行
时,您希望引用已编译的测试类文件,而不是测试Java源文件

我建议将常规源代码和测试源代码放在两个不同的目录中,然后将它们编译到两个不同的目录中。我们使用Maven目录标准:

  • src/main/java
    -常规源文件的位置
  • src/main/resources
    -资源的位置(放入jar的XML和JSON文件)
  • src/test/java
    -测试源文件的位置
  • src/test/resources
    -测试所需资源的位置
  • target/classes
    -编译常规源类文件的地方
  • 目标/测试类
    -编译测试类文件的地方
例如:

<target name="compile">
    <javac srcdir="src/main/java"
        destdir="target/classes">
        <classpath ref="main.classpath"/>
    </javac>
</target>

<target name="package"
    depends="compile">
    <jar destfile="target/${ant.project.name}.jar">
        <fileset dir="target/classes"/>
        <fileset dir="src/main/resources"/>
    </jar>
</target>

<target name="compile-test"
    depends="compile">

    <!-- Classpath must include the main.classpath from -->
    <!-- above, the test.classpath which will include   -->
    <!-- "junit.jar", and finally the compiled classes  -->
    <!-- from target/classes. Not 100% sure if this is  -->
    <!-- the correct way to specify it, but it's close. -->
    <javac srcdir="src/test/java"
        destdir="target/test-classes">
        <classpath>
             <path ref="main.classpath"/>
             <path ref="test.classpath"/>
             <path location="target/classes"/>
        <classpath>
    </javac>
</target>

<junit printsummary="yes"
    haltonfailure="yes"
    showoutput="yes">
    <classpath>
        <path refid="main.classpath" />
        <path refid="test.classpath" />
        <path location="${classes.dir}" />
    </classpath>

        <!-- Batchtest is pointing to the classes-->
        <formatter type="xml" />
        <batchtest fork="yes">
            <fileset dir="${target/test-classes}"/>
        </batchtest>
    </junit>
</target>

这是有效的,是@dawid-w和@dkatzel答案的组合

        <batchtest fork="yes" skipnontests="yes" todir="${report.dir}">
            <fileset dir="${classes.dir}" />
        </batchtest>

这是有效的,是@dawid-w和@dkatzel答案的组合

        <batchtest fork="yes" skipnontests="yes" todir="${report.dir}">
            <fileset dir="${classes.dir}" />
        </batchtest>


它确实将TESTS-TestSuits.xml输出到build/junitreport中,在那里它应该是正确的。它确实将TESTS-TestSuits.xml输出到build/junitreport中,在那里它应该是正确的,所以它似乎是正确的。我将点改为类,但它仍然不起作用。谢谢你对项目结构的建议,但是当我试图转换成它的时候,很多东西都搞砸了,所以我现在要继续我现在的项目。还有一件事要尝试“使用
ant-d
运行此命令。这将打印出很多内容,所以您需要将其保存到文件中,或者从一个干净的终端和一个大的终端缓冲区开始。您可以查看是否实际执行了
batchtest
行,或者正在执行
junit
任务,以及发生了什么。这是
-d
调试标志的输出吗?你能看到目录
/Users/richard/\u GIT/slot machine/build/classes
中有什么吗?查看是否有与
*Test.class
后缀匹配的文件?另外,更新您的问题以显示build.xml文件中的更改。我发布了我自己的答案,它是您的答案、给出的另一个答案和我阅读的一些文档的组合。我将问题更改为指向类,但仍然不起作用。谢谢你对项目结构的建议,但是当我试图转换成它的时候,很多东西都搞砸了,所以我现在要继续我现在的项目。还有一件事要尝试“使用
ant-d
运行此命令。这将打印出很多内容,所以您需要将其保存到文件中,或者从一个干净的终端和一个大的终端缓冲区开始。您可以查看是否实际执行了
batchtest
行,或者正在执行
junit
任务,以及发生了什么。这是
-d
调试fla的输出吗