【问题标题】:ClassNotFoundException when running ant junit运行 ant junit 时出现 ClassNotFoundException
【发布时间】:2014-07-23 15:47:05
【问题描述】:

我在运行下面的 Junit 任务时遇到了这个ClassNotFoundException 问题,但这是从命令提示符运行 ant 构建的非基于 Eclipse 的项目,构建文件粘贴在下面。例外如下。我已经完成了与设置here 类似的操作,但它不起作用,任何指针将不胜感激。谢谢。

Class org.apache.tools.ant.util.StringUtils loaded from parent loader (parentFirst)
    [junit] Testsuite: SampleTest
Class org.apache.tools.ant.util.FileUtils loaded from parent loader (parentFirst)
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] 
    [junit]     Caused an ERROR
    [junit] SampleTest
    [junit] java.lang.ClassNotFoundException: SampleTest
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    [junit]     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:219)
    [junit] 

Build.xml

<project name="Java project build" default="test">
    <property name="project.local.directory" value="." />
    <property name="src.path" value="${project.local.directory}/SampleUnitTest/com" />
    <property name="lib.path" value="${project.local.directory}/APP-INF/lib" />
    <property name="dest.path" value="${project.local.directory}/SampleUnitTest/target" />
    <property name="junit.output.dir" value="${project.local.directory}/junit" />

    <path id="MyProject.classpath">
        <pathelement location="${lib.path}/ant-junit.jar" />
        <pathelement location="${lib.path}/junit.jar" />        
        <pathelement location="${lib.path}/SampleUnitTest.jar" />           
        <dirset dir="${project.local.directory}/SampleUnitTest">
            <include name="target" />
        </dirset>       
    </path>
    <path id="lib.classpath">
        <pathelement location="${lib.path}/ant-junit.jar" />
        <pathelement location="${lib.path}/junit.jar" />
        <fileset dir="${lib.path}">
            <include name="**/*.jar" />
        </fileset>
    </path>
    <target name="test" description="Tests the java files" depends="build">
        <mkdir dir="${junit.output.dir}" />
        <junit>
            <classpath refid="MyProject.classpath">
            </classpath>
            <batchtest todir="${junit.output.dir}">
                <formatter type="plain" usefile="false" />
                <fileset dir="${src.path}">
                    <include name="**/*Test*.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="build" description="Tests the java files" depends="clean">
        <mkdir dir="${dest.path}" />
        <javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath">
        </javac>        
        <jar destfile="${lib.path}/SampleUnitTest.jar" basedir="${dest.path}"/>
    </target>

    <target name="clean" description="Tests the java files">
        <delete dir="${dest.path}">
        </delete>
    </target>
</project>

更新

package com; 

import com.tds.metasolv.common.util.CommonUtilities;
import com.tds.metasolv.common.util.specific.PortAddressUtilities;
import junit.framework.TestCase;

public class SampleTest extends TestCase
{ 
    PortAddressUtilities utils = null;

    protected void setUp() throws Exception{
        utils = PortAddressUtilities.getInstance(CommonUtilities.getInstance());
    }

    public void testGetPONPortNumber(){
        String result = utils.getPONPortNumber("N10-1-2-3-4");
        assertTrue("Expected 3, but got "+result +" in the result. ", result.equals("3"));
        result = utils.getPONPortNumber("N10-1-2-3");
        assertTrue("Expected 3, but got "+result +" in the result. ",result.equals("2"));
    }
} 

【问题讨论】:

    标签: java ant junit


    【解决方案1】:

    问题是你定义的src.dir

    <property name="src.path" value="${project.local.directory}/SampleUnitTest/com"/>
    

    您创建的类在package com 中,这意味着将使用com.SampleTest 引用该类

    为了修复这个异常,您需要添加一个 src 文件夹并将您的 com/Test.java 移动到该文件夹​​,以便上面定义的 src.path 会像

    <property name="src.path" value="${project.local.directory}/SampleUnitTest/src"/>
    

    并在构建 xml 中更改 MyProject.classpath,如下所示

    <path id="MyProject.classpath">
        <pathelement location="${lib.path}/ant-junit.jar" />
        <pathelement location="${lib.path}/junit.jar" />        
        <pathelement location="${lib.path}/SampleUnitTest.jar" />           
        <pathelement location="${project.local.directory}/SampleUnitTest/target"/>
    </path>
    

    【讨论】:

    • 请发布 SampleTest.java
    • 是的,这就是问题所在。但是,我做了一点不同,我将 SampleUnitTest 作为源文件夹,并从该文件夹中删除了所有其他内容,将所有构建路径向上移动。
    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多