运行 ant junit 时出现 ClassNotFoundException [英] ClassNotFoundException when running ant junit

查看:24
本文介绍了运行 ant junit 时出现 ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行下面的 Junit 任务时遇到了这个 ClassNotFoundException 问题,但这是从命令提示符运行 ant 构建的非基于 Eclipse 的项目,构建文件是贴在下面.例外情况如下.我做了类似于设置here的设置,但它不起作用,任何指针都会受到赞赏.谢谢.

I'm running into this ClassNotFoundException issue when running Junit task below, but this is non eclipse based project running an ant build from command prompt, the build file is pasted below. The exception is below. I have done similar to that of setup here it does not work though, any pointers will be appreciated. Thanks.

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"));
    }
} 

推荐答案

问题是你定义的 src.dir

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

您创建的类位于 package com 中,这意味着将使用 com.SampleTest

The class you have created is in package com that means the class will be referenced using com.SampleTest

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

In order to fix this anomaly you need to add a src folder and move your com/Test.java to that folder so that the above defined src.path will be like

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

并且在 build xml 中更改 MyProject.classpath 如下

And also change MyProject.classpath in build xml as below

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

这篇关于运行 ant junit 时出现 ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆