以前只使用过SBT和Maven,我无法让Ant使用Junit测试。我已经在SO上查看了无数与此相关的其他类似问题,但无法解决。这些测试在从 Eclipse 内部运行时运行良好。失败的测试类抛出"NoClassDefFoundError"
我的 Ant 构建.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)
此时的任何指示或提示都会很棒。
Hank 关于路径元素/位置的提示使我发现以下内容有效。
现在将test.classpatch id更改为以下内容将获取所有必要的应用程序和测试库。
<path id="test.classpath">
<pathelement location="${classes}" />
<fileset dir="${classpath}" includes="**/*.jar"/>
<fileset dir="${classpath.test}" includes="**/*.jar"/>
</path>
非常感谢汉克!