ClassNotFoundException when running ant junit

2019-09-14 20:01发布

问题:

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>

Update

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

回答1:

Problem is the src.dir you have defined

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

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

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

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>


标签: java ant junit