Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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
Ant-Junit-eclipse-build.xml-java.lang.ClassNotFoundException错误_Ant - Fatal编程技术网

Ant-Junit-eclipse-build.xml-java.lang.ClassNotFoundException错误

Ant-Junit-eclipse-build.xml-java.lang.ClassNotFoundException错误,ant,Ant,我是Ant的新手,获得java.lang.ClassNotFoundException。但是,当从eclipse/Ant build.xml运行Junit时,当从eclipse单独运行单元测试时,测试通过,没有问题。我的类路径应该有一些问题,我无法解决 我的环境是: Java_home:C:/ProgramFiles/Java/jdk1.7.0_25 Ant_home:C:/Users/Armen/javaFolder/apache-Ant-1.9.2/bin JUnit_home:C:/Use

我是Ant的新手,获得java.lang.ClassNotFoundException。但是,当从eclipse/Ant build.xml运行Junit时,当从eclipse单独运行单元测试时,测试通过,没有问题。我的类路径应该有一些问题,我无法解决

我的环境是:

Java_home:C:/ProgramFiles/Java/jdk1.7.0_25 Ant_home:C:/Users/Armen/javaFolder/apache-Ant-1.9.2/bin JUnit_home:C:/Users/Armen/javaFolder/apache-ant-1.9.2/bin/JUnit-4.10.jar

My Build.xml

<property name="junitLocation">C:/Users/Armen/javaFolder/apache-ant-1.9.2/bin/junit-4.10.jar</property>
<property name="antLocation2">C:/Users/Armen/JavaFolder/apache-ant-1.9.2.jar</property>
<property name="testCode">C:/Users/Armen/workspace/MockingObjects/test/demo</property>
<property name="srcCode">C:/Users/Armen/workspace/MockingObjects/src/demo</property>

<target name="compile" depends="clean">
        <javac includeantruntime="false" srcdir="./src" destdir="./staging" ></javac>
</target>

<target name="run" depends="compile, unitTest">
    <java classname="demo.AccountService"><classpath path="./staging"></classpath></java>
</target>

<target name="unitTest" depends="compile">
    <junit printsummary="true" showoutput="true" haltonfailure="false" fork="yes">

        <formatter type="plain" usefile="true"/>
        <test name="demo.TestAccountService" outfile="./result" ></test>

        <classpath> 
            <pathelement location="${junitLocation}"/>
            <pathelement location="${antLocation}"/>
            <pathelement location="${testCode}" />
            <pathelement location="${srcCode}"/>
        </classpath>

    </junit>
</target>

<target name="clean">
    <delete dir="staging"></delete>
    <mkdir dir="./staging"/>
</target>

C:/Users/Armen/javaFolder/apache-ant-1.9.2/bin/junit-4.10.jar
C:/Users/Armen/JavaFolder/apache-ant-1.9.2.jar
C:/Users/Armen/workspace/MockingObjects/test/demo
C:/Users/Armen/workspace/MockingObjects/src/demo

在此处输入image description

您的构建脚本中没有编译单元测试的任何步骤,并且当您告诉JUnit运行时,您也不会在类路径中包含编译后的类—只有源文件

因此,您需要做两件事:

  • 添加一个类似于以下内容的额外构建步骤

    <target name="test-compile" depends="compile">
        <javac includeantruntime="false" srcdir="./test" destdir="./test-classes" />
    </target>
    

  • 注意在编译和运行步骤中,不清楚您的代码是在Java包中正确构造的,还是在包中正确构造的,您是否理解这些代码是如何工作的。我假设您的代码位于
    demo
    包中,因此将该部分路径从编译的类位置中剥离出来。

    哪个类NotFoundException?检查类并在类路径中找到相同的jar文件?
    <property name="testCode">C:/Users/Armen/workspace/MockingObjects/test-classes</property>
    <property name="srcCode">C:/Users/Armen/workspace/MockingObjects/staging</property>