日期:2014-05-20  浏览次数:20826 次

用Ant执行Junit Test,失败,找不到class.
写了一个ant build.xml来执行这个Junit测试,如下:

XML code

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="test" name="Test TestSmokeTestForServer">
    <import file="../BinaryRepositoryWS/build.xml"/>
    <property name="TESTDIR" value="."/>
    <property name="TEST-DEBUG" value="${TESTDIR}/ant-bin-debug"/>
    <property name="REPORTS-DIR" location="reports"/>
    <property name="LIB" value="lib" />
    <property name="SRC" value="src"/>
    <property name="JAVA_COMPILER_VERSION" value="1.6"/>
    <property name="DEBUG" value="false" />
    <property name="BUILD-DIR" value="ant-bin"/>
    <target name="test" depends="compile-test, run-test" />
    <target name="compile-test" depends="clean, mkdirs, compile"/>
    <target name="compile" depends="mkdirs">
        <javac target="${JAVA_COMPILER_VERSION}" srcdir="${SRC}" destdir="${BUILD-DIR}" debug="${DEBUG}">
        <classpath refid="webservice-classpath"/>
        <classpath refid="webservice-test-classpath"/>
        <classpath location="${BUILD-DIR}"/>
        </javac>
    </target>
    <target name="clean">
        <delete dir="${TEST-DEBUG}" />
        <delete dir="${REPORTS-DIR}" />
        <delete dir="${BUILD-DIR}"/>
    </target>
    <target name="mkdirs">
        <mkdir dir="${TEST-DEBUG}"/>
        <mkdir dir="${REPORTS-DIR}"/>
        <mkdir dir="${BUILD-DIR}"/>
    </target>
    <target name="run-test" depends="">
        <junit fork="yes" forkmode="perTest" printsummary="yes" haltonfailure="no" haltonerror="false" showoutput="true" >
        <classpath location="${TEST-DEBUG}"/>
        <classpath location="${BUILD-DIR}"/>
        <classpath location="${LIB}"/>
        <formatter type="xml" />
        <test name= "com.sonyericsson.binaryreposystem.client.TestSmokeTestForServer" />
        </junit>
    </target>
</project>



但是还是报错,如下:
  <testcase classname="com.sonyericsson.binaryreposystem.client.TestSmokeTestForServer" name="initializationError" time="0.0060">
  <error message="org/hamcrest/SelfDescribing" type="java.lang.NoClassDefFoundError">java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing