【发布时间】: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"));
}
}
【问题讨论】: