简体   繁体   中英

Use Ant to build junit test with initializationError: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

This is failure trace from junit test report:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

This is my build.xml:

<project name="Ant-Test-JUnit" default="main">
<description>
        JUnit Test on MyMath
</description>

<property name="src.dir" location="src"/>
<property name="bin.dir" location="bin"/>

<!-- Variables used for JUnit testin -->
<property name="test.dir" location="src" />
<property name="test.report.dir" location="testreport" />

<path id="test.classpath">
    <pathelement location="${bin.dir}" />
    <pathelement location="../../../../plugins/org.junit_4.11.0.v201203080030/junit.jar" />     
    <pathelement location="../../../../plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>
</path>

<!-- ================================= 
      target: main             
     ================================= -->
<target name="main" depends="compile, junit">
    <echo message="Junit Test :)"/>
</target>

<!-- - - - - - - - - - - - - - - - - - 
      target: compile                      
     - - - - - - - - - - - - - - - - - -->
<target name="compile" depends="clean, makedir">
    <javac includeantruntime="false" srcdir="${src.dir}" destdir="${bin.dir}">
        <classpath refid="test.classpath"/>
    </javac>
</target>

<!-- - - - - - - - - - - - - - - - - - 
              target: clean, makedir                   
     - - - - - - - - - - - - - - - - - -->
<target name="clean">
    <delete dir="${bin}"/>
    <delete dir="${test.report.dir}" />
</target>

<target name="makedir">
    <mkdir dir="${bin.dir}"/>
    <mkdir dir="${test.report.dir}" />
</target>

<!-- - - - - - - - - - - - - - - - - - 
          target: junit                      
     - - - - - - - - - - - - - - - - - -->
<target name="junit" depends="compile">
    <junit printsummary="yes" fork="true" haltonfailure="yes">
        <classpath refid="test.classpath" />
        <formatter type="xml" />
        <batchtest todir="${test.report.dir}">
            <fileset dir="${src.dir}">
              <include name="**/*UnitTest*.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

I've added Junit_4.11 jar to Preference->Ant->Runtime->Global Entries and package org.hamcrest.* to Preference->JUnit->Add Packages(but this doesn't fix the problem of NoClassDefFoundError:org/hamcrest/SelfDescribing)

If I run UnitTest*.java using JUnit plugin in eclipse directly, it works and shows all tests passed.

Could anyone help me figure out the solution? Thank you.

Found the problem. This is caused by wrong path location. I just changed the path element to the right location and solve it. Ant just needs correct path. Each time check if the location is right.

replace

<pathelement location="../../../../plugins/org.junit_4.11.0.v201203080030/junit.jar" />     
<pathelement location="../../../../plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>

with

<pathelement location="/Applications/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar"/>
<pathelement location="/Applications/eclipse/plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>

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