之前只使用过SBT和Maven,我无法让Ant与Junit测试一起工作。我已经看过无数其他与此相关的类似问题但是无法解决。从Eclipse内部运行时,这些测试运行良好。失败的测试类抛出“NoClassDefFoundError”
My Ant build.xml片段:
<project name="APPbuild" basedir=".">
<description>APP build</description>
<property name="webinf" location="WEB-INF" />
<property name="src" location="${webinf}${file.separator}src" />
<property name="src.test" location="${webinf}${file.separator}test" />
<property name="classes" location="${webinf}${file.separator}classes" />
<property name="classes.test" location="${webinf}${file.separator}classestest" />
<property name="classes.instrument" location="${webinf}${file.separator}classesinstrument" />
<property name="dist" location="..${file.separator}dist" />
<property name="archiveFile" location="${dist}${file.separator}app.war" />
<property name="classpath" location="${webinf}${file.separator}lib" />
<property name="classpath.test" location="${webinf}${file.separator}resources${file.separator}test" />
<property name="junit.output.dir" value="${webinf}${file.separator}junit" />
<path id="libpath">
<fileset dir="${classpath}">
<include name="**${file.separator}*.jar" />
</fileset>
</path>
<path id="libpath_test">
<fileset dir="${classpath}">
<include name="**${file.separator}*.jar" />
</fileset>
<fileset dir="${classpath.test}">
<include name="**${file.separator}*.jar" />
</fileset>
</path>
<path id="test.classpath">
<pathelement location="${src}" />
<pathelement location="${src.test}" />
<pathelement location="${classes}" />
<pathelement location="${classpath}" />
<pathelement location="${classpath.test}" />
<pathelement location="${webinf}${file.separator}resources${file.separator}test${file.separator}junit-4.7.jar" />
</path>
<target name="compile" depends="init,prepare">
<javac classpathref="libpath" debug="on" srcdir="${src}" destdir="${classes}" includeantruntime="false" />
</target>
<target name="compile_test" depends="compile">
<javac classpathref="libpath_test" srcdir="${src.test}" destdir="${classes}" includeantruntime="false" />
</target>
<target name="test" depends="compile_test">
<mkdir dir="${junit.output.dir}" />
<junit fork="true" printsummary="true" showoutput="true">
<formatter type="xml" />
<classpath refid= "test.classpath" />
<batchtest todir="${junit.output.dir}">
<fileset dir="${classes}">
<include name="**/*Test.class" />
</fileset>
</batchtest>
</junit>
<echo>${classpath}</echo>
<echo>${classpath.test}</echo>
<echo>${classes}</echo>
<echo>${classes.test}</echo>
</target>
其中一个测试类
出错java.lang.NoClassDefFoundError: javax/mail/Address
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
另一个
java.lang.NoClassDefFoundError: org/dbunit/database/IDatabaseConnection
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.dbunit.database.IDatabaseConnection
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
此时的任何指针或提示都会很棒。
答案 0 :(得分:0)
将test.classpatch id更改为以下内容现在可以获取所有必需的应用程序并测试lib。
<path id="test.classpath">
<pathelement location="${classes}" />
<fileset dir="${classpath}" includes="**/*.jar"/>
<fileset dir="${classpath.test}" includes="**/*.jar"/>
</path>
非常感谢汉克!