简体   繁体   English

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

[英]Junit test fails but build is successful (Ant)

Hi i'm quite new to using ant, my instructor wanted us to use it to do some tasks like build the class, run tests..etc嗨,我对使用 ant 很陌生,我的老师希望我们用它来完成一些任务,例如构建课程、运行测试等

and i am really confused since i cant make the junit work.我真的很困惑,因为我无法让 junit 工作。 i have read several other posts and sources but it makes me even more confused.我已经阅读了其他几篇文章和资料来源,但这让我更加困惑。

the error i am getting is:我得到的错误是:

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)

this is my build file:这是我的构建文件:

<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>

You should use <junit haltOnFailure="yes" haltOnError="yes" ...> if you want build to fail.如果您希望构建失败,您应该使用<junit haltOnFailure="yes" haltOnError="yes" ...>

Alternatively you can use failureProperty and errorProperty或者,您可以使用failurePropertyerrorProperty

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

And verify if those properties was set并验证是否设置了这些属性

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM