我在下面运行 Junit 任务时遇到此ClassNotFoundException
问题,但这是基于非eclipse的项目,从命令提示符运行ant build,构建文件被粘贴下面。例外情况如下。我已经完成了类似于setup 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"));
}
}
答案 0 :(得分: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>