简体   繁体   English

jUnit 测试在 Eclipse 中工作,但是当我通过 ant 运行它们时失败

[英]jUnit tests work in Eclipse, but fail when I run them through ant

I have the following tasks in ant:我在 ant 中有以下任务:

<target name="init-junit" depends="init">
    <mkdir dir="${junit.reports.individual}" />
    <property name="running-junit" value="true" />
</target>

<target name="run-tests" depends="init-junit, compile">
    <junit>
        <classpath refid="classpath" />
        <formatter type="xml" />
        <batchtest todir="${junit.reports.individual}">
            <fileset dir="${dir.build}" includes="**/*Test*" />
        </batchtest>
    </junit>
</target>

<target name="compile-reports" depends="run-tests">
    <junitreport todir="${junit.reports}" tofile="junit-report.xml">
        <fileset dir="${junit.reports.individual}" />
        <report format="frames" todir="${junit.reports}/html" />
    </junitreport>
</target>

with ${dir.build} being the directory with all my .class files. ${dir.build}是我所有.class文件的目录。 The jUnit tests work when I run them in eclipse, but fail when I run them through ant (either run through eclipse or terminal); The jUnit tests work when I run them in eclipse, but fail when I run them through ant (either run through eclipse or terminal); they each throw the following exception:他们每个人都抛出以下异常:

org.fscit.{name of class}
    java.lang.ClassNotFoundException: org.fscit.{name of class}
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)

I have junit-4.8.2.jar in my lib folder, which is in the classpath with the id, classpath , and I have build.xml on my project root directory, with its basedir property set to .我的lib文件夹中有junit-4.8.2.jar ,该文件夹位于 id 为classpath的类路径中,并且我的项目根目录中有 build.xml ,其basedir属性设置为. . . Can anyone help me?谁能帮我?

You need to have the destination directory of your classes in the classpath.您需要在类路径中有类的目标目录。 You should have ${dir.build} in your classpath element.您的类路径元素中应该有${dir.build}

<path id="classpath">
<pathelement location="${dir.build}"/>

Use pathelement task使用路径元素任务

<pathelement location="${dir.build}" /> 

instead of fileset task而不是文件集任务

<fileset dir="${dir.build}" />

It works for me (ant version is 1.7.1)它对我有用(蚂蚁版本是 1.7.1)

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

相关问题 运行在Eclipse中运行的JUnit测试时,Ant抛出ClassNotFoundException - Ant throws ClassNotFoundException when running JUnit tests that work in Eclipse jUnit测试在Eclipse中工作,但在ant中失败 - java.lang.RuntimeException:Stub - jUnit tests work in Eclipse, but fail in ant - java.lang.RuntimeException: Stub Ant和Junit所有测试均失败 - Ant and Junit all tests fail 如果我通过Eclipse中的ANT运行测试用例,则不会在JUNIT文件夹中生成JUnit HTML报告(index.html)。 - JUnit HTML report (index.html) is not generated in the JUNIT folder, If I run the test case through ANT in eclipse 我的JUnit测试在Eclipse中运行时有效,但有时会通过Ant随机失败 - My JUnit tests works when run in Eclipse, but sometimes randomly fails via Ant 为什么我的 Mockito-inline JUnit 测试在运行 Ant 时会崩溃,并在 IntelliJ IDEA 中成功? - Why do my Mockito-inline JUnit tests crash when run through Ant, and succeed in IntelliJ IDEA? 当我按包运行 mvn 测试时,JUnit 测试通过,但是当我将所有包一起运行时,一些测试失败 - JUnit tests pass when I run mvn tests by packages, but when I run all the packages together some of the tests fail 所有JUnit测试运行时的Eclipse通知 - Eclipse notification when all JUnit tests run Ant,运行所有jUnit测试 - Ant, run all jUnit tests 使用Ant运行多个JUnit测试 - Run multiple JUnit tests with Ant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM