java.lang.ClassNotFoundException:main.java.KmToMileTest

时间:2016-05-04 21:44:52

标签: java junit ant classnotfoundexception

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

1 个答案:

答案 0 :(得分:1)

我看到了很多问题:

  1. 您将编译器的源目录设置为src,应该是src/main/java
  2. 您的包与您的文件夹结构不符(kmToMilesTest包含prject的包(这就是为什么这些类位于bin/prject而不是bin) ,但不在src/main/java/prject文件夹中。
  3. 您的测试名称不匹配;您尝试运行测试类main.java.KmToMileTest,但您的测试类名为prject.kmToMilesTest