Junit测试失败但构建成功(Ant)

时间:2016-09-13 22:20:44

标签: java junit ant

嗨,我很擅长使用ant,我的导师希望我们用它来完成一些任务,比如构建类,运行tests ..etc

我真的很困惑,因为我不能让junit工作。我已经阅读了其他一些帖子和来源,但这让我更加困惑。

我得到的错误是:

Testsuite: SolutionTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec

Caused an ERROR
SolutionTest
java.lang.ClassNotFoundException: SolutionTest
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)

这是我的构建文件:

<project name="Solution" 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="doc.dir" value="${build.dir}/documentation" />
    <property name="test.dir" value="${build.dir}/test" />      

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
            <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
        </javac>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
            <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        </jar>
    </target>

    <target name="clean" depends="jar">
        <delete dir="${classes.dir}"/>
    </target>

    <target name="doc" depends="clean">
        <mkdir dir="${doc.dir}" />
        <javadoc destdir="${build.dir}/documentation" sourcefiles="${src.dir}/*.java" />
    </target>

    <target name="junit" depends="doc">
        <mkdir dir="${test.dir}" />
        <junit printsummary="yes" haltonfailure="no">
            <classpath location="." />

            <test name="SolutionTest" haltonfailure="no" todir="${test.dir}" outfile="result">
                <formatter type="plain" />
                <formatter type="xml" />
            </test>
        </junit>
    </target>

    <target name="main" depends="clean, junit"/>
</project>

1 个答案:

答案 0 :(得分:3)

如果您希望构建失败,则应使用<junit haltOnFailure="yes" haltOnError="yes" ...>

或者,您可以使用failurePropertyerrorProperty

<junit failureProperty="test.failure" haltonfailure="test.error">
    <!-- your code here -->
</junit>

并验证是否已设置这些属性

<target name="main" depends="clean, junit, verifyNoError, verifyNoFailure"/>

<target name="verifyNoError" if="test.error">
    <fail message="JUnit test or tests errors."/>
</target>

<target name="verifyNoFailure" if="test.failure">
    <echo message="I'm here. Now what?"/>
    <fail message="JUnit test or tests failed."/>
</target>