【问题标题】:ClassNotFoundException when runing JUnit tests using Ant使用 Ant 运行 JUnit 测试时出现 ClassNotFoundException
【发布时间】:2015-11-13 16:18:06
【问题描述】:

我正在尝试使用 Ant 脚本运行一些 JUnit 测试。我可以在 Eclipse 中运行它们,但是在使用 Ant 时遇到了问题。

我有一个简单的类,叫做 Calculator,它会做一些计算,这是我的类:

Calculator.java

package CalculatorSrc;

public class Calculator {

    public double add(double n1, double n2){
        return n1+n2;
    }   

    //...

}

然后我有另一个类来测试我的方法,例如:

TestCalculator.java

package CalculatorTests;

import static org.junit.Assert.*;

import org.junit.*;

import CalculatorSrc.Calculator;

public class TestCalculator {

    @Test
    public void testAdd(){
        Calculator calc = new Calculator();
        double result = calc.add(10, 50);
        assertEquals(60, result, 0);
    }   

    //...

}

我的项目目录是这样的:

最后,我运行 JUnit 测试的 Ant 脚本是这样的:

<property name="build.classes.dir" location="build/classes"/>

<property name="class.src.dir" location="src/CalculatorSrc"/>
<property name="tests.src.dir" location="src/CalculatorTests"/>
<property name="reports.dir" location="reports"/>

<path id="classpath.base" />

<path id="test.classpath">
    <pathelement location="${class.src.dir}"/>
    <pathelement location="${tests.src.dir}"/>
    <pathelement location="${build.classes.dir}"/>
    <pathelement location="tools/JUnit/junit-4.12.jar"/>
    <pathelement location="tools/JUnit/hamcrest-core-1.3.jar"/>
    <pathelement location="tools/JUnit/ant-junit-1.9.4.jar"/>
</path>

<target name="build">
    <antcall target="test-compile"/>
    <antcall target="test"/>
</target>

<target name="test-compile">
    <javac includeantruntime="false" srcdir="${class.src.dir}" destdir="${build.classes.dir}" debug="true">
        <classpath refid="classpath.base"/>
    </javac>
    <javac includeantruntime="false" srcdir="${tests.src.dir}" destdir="${build.classes.dir}" debug="true">
        <classpath refid="test.classpath"/>
    </javac>
</target>

<target name="test">
    <junit haltonfailure="no" failureproperty="failed">
        <classpath>
            <path refid="test.classpath"/>
            <pathelement location="${build.classes.dir}"/>
        </classpath>
        <formatter type="xml"/>
        <batchtest fork="yes" todir="${reports.dir}">
            <fileset dir="${tests.src.dir}" includes="**/*Test*.java"/>
        </batchtest>
    </junit>
    <fail message="TEST FAILURE" if="failed"/>
</target>

当我使用 Jenkis 运行 Ant 脚本时,我的构建失败...如果我检查创建的 xml 文件,我会看到错误 ClassNotFoundException...

我做错了什么?

编辑:

以下是我构建项目时 Jenkins 的输出结果:

正如您在下一个屏幕截图中看到的那样,jenkins 可以找到我的 build.xml...

这是我遇到的错误:

我的 build.xml 的第 57 行是&lt;antcall target="test"/&gt;,第 121 行是&lt;fail message="TEST FAILURE" if="failed"/&gt;

【问题讨论】:

  • 你如何启动 ant?
  • 我使用 jenkins 执行它。
  • Jenkins 使用哪个命令?
  • 不知道,我用jenkins的接口跑Ant。在 jenkis 中,我唯一要做的就是创建一个项目,提供 build.xml 位置然后开始构建,我在构建项目时添加了一些输出打印。

标签: ant junit classpath


【解决方案1】:

您使用错误的目录和包。 src/CalculatorSrcsrc/CalculatorTests 都是源文件夹,但在 Eclipse 中,您使用父目录 src 作为源文件夹。怎么办:

  • 在 Eclipse 中更改项目设置:src/CalculatorSrcsrc/CalculatorTests 都是源文件夹
  • Calculator.javaCalculatorTest.java(每个文件的第一行)中删除包声明
  • CalculatorTest.java 中删除import CalculatorSrc.Calculator;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2015-08-07
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    相关资源
    最近更新 更多