简体   繁体   中英

One more Ant/junit ClassNotFoundException

I have two identical build files that look like this

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Tryout" default="test1" basedir="."> 
    <property name="src" location="src"/>
    <property name="build" location="bin"/>
    <property name="junit" location="junit-4.10.jar"/>
    <target name="compile" description="compile the source" >
        <javac srcdir="${src}" destdir="${build}" debug="on">
            <classpath location="junit-4.10.jar"/>
            <classpath location="${build}"/>
        </javac>
    </target>
    <target name="test1" depends="compile">
        <junit printsummary="yes" fork="no" haltonfailure="no">
            <classpath location="${build}"/>
            <classpath location="junit-4.10.jar"/> 
            <formatter type="plain"/>
            <test name="TestOne"/> 
            <test name="TestTwo"/>
        </junit>
    </target>
</project>

The only difference is in Trial1 the classes are in bin , while in Trial2 the classes are in bin/one/two/three and all the classes are in the package one.two.three . Trial1 works fine the junit tests run and print the results to a txt file. Trial2 is able to compile the source to the appropriate folder but then fails when junit executes. Here is the error:

Testsuite: TestOne
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

    Caused an ERROR
TestOne
java.lang.ClassNotFoundException: TestOne
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)

Here's part of the debugging that I think is relevant

Class java.util.ArrayList loaded from parent loader (parentFirst)
Finding class org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner$3
Loaded from /usr/share/ant/lib/ant-junit.jar org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner$3.class
Finding class TestOne
Class java.lang.System loaded from parent loader (parentFirst)
Class java.lang.StringBuffer loaded from parent loader (parentFirst)
    [junit] Running TestOne
Class org.apache.tools.ant.util.StringUtils loaded from parent loader (parentFirst)
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] Test TestOne FAILED

Any help to point me in a right direction would be greatly appreciated!! Thank you for your time.

Should be <test name="one.two.three.TestOne"/> becaused name of your test class is one.two.three.TestOne .

If you want Ant to find where tests are located, use something like this:

<batchtest>
  <fileset dir="${src}">
    <include name="**/Test*.java"/>
  </fileset>
</batchtest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM