java.lang.ClassNotFoundException: main.java.KmToMileTest
java.lang.ClassNotFoundException: main.java.KmToMileTest
我在 OSX 终端中使用 Ant 进行 运行 JUnit 测试时遇到问题。
我知道问题一定出在类路径上,或者我弄乱了我的文件目录,请帮忙!
我遇到的错误:
test1:
[junit] Running main.java.KmToMileTest
[junit] Testsuite: main.java.KmToMileTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] main.java.KmToMileTest
[junit] java.lang.ClassNotFoundException: main.java.KmToMileTest
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:264)
[junit]
[junit] Test main.java.KmToMileTest FAILED
BUILD SUCCESSFUL
这是我的 build.xml:
<project name="MyProject" default="test1" basedir=".">
<description>
Ant build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="junit" location="junit/"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory -->
<mkdir dir="${bin}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${bin} -->
<javac includeantruntime="false"
srcdir="${src}"
destdir="${bin}"
debug="on">
<classpath location="${junit}/junit-4.11.jar"/>
<classpath location="${junit}/hamcrest-all-1.3.jar"/>
</javac>
</target>
<target name="test1" depends="compile">
<!-- Run junit tests -->
<junit printsummary="yes" fork="yes">
<classpath location="${bin}"/>
<classpath location="${junit}/junit-4.11.jar"/>
<classpath location="${junit}/hamcrest-all-1.3.jar"/>
<formatter type="plain" usefile="false" /> <!-- to screen -->
<formatter type="plain" /> <!-- to file -->
<test name="main.java.KmToMileTest"/>
</junit>
</target>
<target name="clean">
<!-- Delete the ${bin} folder -->
<delete dir="${bin}"/>
</target>
</project>
我的文件目录:
project:
build.xml
junit/
hamcrest-all-1.3.jar
junit-4.11.jar
bin/prject/
lengthConverter.class
kmToMilesTest.class
milesToKmTest.class
parameterizedTest.class
src/main/java/
lengthConverter.java
kmToMilesTest.java
milesToKmTest.java
parameterizedTest.java
我看到了一些问题:
- 您将编译器的源目录设置为
src
,而它应该是 src/main/java
- 您的包与您的文件夹结构不匹配(看起来
kmToMilesTest
有一个 prject
的包(这就是 classes 在 bin/prject
中的原因bin
),但不在 src/main/java/prject
文件夹中)。
- 您的测试名称不匹配;您尝试 运行 测试 class
main.java.KmToMileTest
,但您的测试 class 被命名为 prject.kmToMilesTest
我在 OSX 终端中使用 Ant 进行 运行 JUnit 测试时遇到问题。 我知道问题一定出在类路径上,或者我弄乱了我的文件目录,请帮忙!
我遇到的错误:
test1:
[junit] Running main.java.KmToMileTest
[junit] Testsuite: main.java.KmToMileTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] main.java.KmToMileTest
[junit] java.lang.ClassNotFoundException: main.java.KmToMileTest
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:264)
[junit]
[junit] Test main.java.KmToMileTest FAILED
BUILD SUCCESSFUL
这是我的 build.xml:
<project name="MyProject" default="test1" basedir=".">
<description>
Ant build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="junit" location="junit/"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory -->
<mkdir dir="${bin}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${bin} -->
<javac includeantruntime="false"
srcdir="${src}"
destdir="${bin}"
debug="on">
<classpath location="${junit}/junit-4.11.jar"/>
<classpath location="${junit}/hamcrest-all-1.3.jar"/>
</javac>
</target>
<target name="test1" depends="compile">
<!-- Run junit tests -->
<junit printsummary="yes" fork="yes">
<classpath location="${bin}"/>
<classpath location="${junit}/junit-4.11.jar"/>
<classpath location="${junit}/hamcrest-all-1.3.jar"/>
<formatter type="plain" usefile="false" /> <!-- to screen -->
<formatter type="plain" /> <!-- to file -->
<test name="main.java.KmToMileTest"/>
</junit>
</target>
<target name="clean">
<!-- Delete the ${bin} folder -->
<delete dir="${bin}"/>
</target>
</project>
我的文件目录:
project:
build.xml
junit/
hamcrest-all-1.3.jar
junit-4.11.jar
bin/prject/
lengthConverter.class
kmToMilesTest.class
milesToKmTest.class
parameterizedTest.class
src/main/java/
lengthConverter.java
kmToMilesTest.java
milesToKmTest.java
parameterizedTest.java
我看到了一些问题:
- 您将编译器的源目录设置为
src
,而它应该是src/main/java
- 您的包与您的文件夹结构不匹配(看起来
kmToMilesTest
有一个prject
的包(这就是 classes 在bin/prject
中的原因bin
),但不在src/main/java/prject
文件夹中)。 - 您的测试名称不匹配;您尝试 运行 测试 class
main.java.KmToMileTest
,但您的测试 class 被命名为prject.kmToMilesTest