尝试从ANT运行JUnit测试时java.lang.NoClassDefFoundError

时间:2011-09-07 17:13:59

标签: testing ant junit

尝试使用以下build.xml中的runtestsreport任务从ANT运行我的JUnit测试时遇到java.lang.NoClassDefFoundError问题;我知道其他任务有效,只是测试的实际运行才是问题。

我已根据此处发布的问题尝试了很多调整但我无法修复它;我真正需要的是一个配对的开发者,但他们都已经回家了。

有人可以帮忙吗?

build.xml文件是

<property name="build" location="C:\<path on my machine>\workspace\ESS\build"/>
<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\tests"/>
<property name="frameworkbuild" location="C:\<path on my machine>\workspace\ESS\build\esstestapp\framework"/>
<property name="rawtestreports" location="C:\<path on my machine>\workspace\ESS\reports\raw"/>
<property name="htmltestreports" location="C:\<path on my machine>\workspace\ESS\reports\html"/>
<property name="reports" location="C:\<path on my machine>\workspace\ESS\reports"/>
<property name="src" location="C:\<path on my machine>\workspace\ESS\src"/>
<property name="testsrc" location="C:\<path on my machine>\workspace\ESS\src\esstestapp\tests"/>
<property name="seleniumtools" location="C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2"/>
<property name="junittools" location="C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2"/>

<target name="init">   
    <tstamp/>   
    <mkdir dir="${build}"/>
    <mkdir dir="${rawtestreports}"/>
    <mkdir dir="${htmltestreports}"/>
</target> 

<path id="tools.classpath">
    <fileset dir="${seleniumtools}" includes="**/*.jar"/>   
    <fileset dir="${junittools}" includes="**/*.jar"/>
</path> 

<target name="compile" depends="init" description="compile the source" >    
    <javac srcdir="${src}" destdir="${build}" classpathref="tools.classpath" includeantruntime="false"/>
</target>

<target name="frameworkjar" depends="compile">        
    <jar destfile="${frameworkbuild}/jar/framework.jar" basedir="${frameworkbuild}" />        
</target>

<target name="testsjar" depends="compile">        
    <jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />
</target>

<path id="test.classpath">
    <fileset dir="${seleniumtools}" includes="**/*.jar"/>   
    <fileset dir="${junittools}" includes="**/*.jar"/>
    <fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
    <fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>
</path> 

<target name="runtests" depends="compile, frameworkjar, testsjar" description="runs the tests" >    
    <junit printsummary="yes" haltonfailure="no" showoutput="yes" fork="no"> 
        <classpath refid="test.classpath" />                 

        <batchtest fork="yes" todir="${rawtestreports}"> 
            <formatter type="xml"/> 
                <fileset dir="${testsrc}">
                    <include name="**/*.java"/> 
                </fileset> 
        </batchtest> 
    </junit>    
</target>

<target name="runtestsreport" depends="runtests">
    <junitreport todir="${reports}">
        <fileset dir="${rawtestreports}">
            <include name="TEST-*.xml" /> 
        </fileset>
        <report format="frames" todir="${htmltestreports}" /> 
    </junitreport>
</target>

<target name="clean" description="Clean up">
    <delete dir="${build}"/>    
    <delete dir="${reports}"/>
</target>

我收到此错误(以及每个测试类的此类错误)

Credentials (wrong name: esstestapp/tests/Credentials)

java.lang.NoClassDefFoundError: Credentials (wrong name: esstestapp/tests/Credentials)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)...

我在-verbose模式下运行ant来检查类路径,但我看不出问题(即使我知道它正在盯着我看)

[junit] Implicitly adding C:\Program Files\apache-ant-1.8.2\lib\ant-launcher
.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\apache-ant-1
.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-junit4.jar to C
LASSPATH
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] Executing 'C:\Program Files\Java\jdk1.6.0_25\jre\bin\java.exe' with
arguments:
[junit] '-classpath'
[junit] 'C:\<path on my machine>\workspace\ESS\tools\junit-4.8.2\junit.jar;C:\User
s\waltersj\workspace\ESS\tools\junit-4.8.2\junitsrc.jar;C:\<path on my machine>\worksp
ace\ESS\tools\selenium-2.0rc2\apache-mime4j-0.6.jar;C:\<path on my machine>\workspace\
ESS\tools\selenium-2.0rc2\bsh-1.3.0.jar;C:\<path on my machine>\workspace\ESS\tools\se
lenium-2.0rc2\cglib-nodep-2.1_3.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-codec-1.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2
.0rc2\commons-collections-3.2.1.jar;C:\<path on my machine>\workspace\ESS\tools\seleni
um-2.0rc2\commons-io-2.0.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.
0rc2\commons-lang-2.4.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
commons-logging-1.1.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\
cssparser-0.9.5.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\guava-
r09.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\hamcrest-all-1.1.j
ar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\htmlunit-2.8.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\htmlunit-core-js-2.8.jar;C:\User
s\waltersj\workspace\ESS\tools\selenium-2.0rc2\httpclient-4.0.2.jar;C:\Users\wal
tersj\workspace\ESS\tools\selenium-2.0rc2\httpcore-4.0.1.jar;C:\<path on my machine>\w
orkspace\ESS\tools\selenium-2.0rc2\httpmime-4.0.1.jar;C:\<path on my machine>\workspac
e\ESS\tools\selenium-2.0rc2\jcommander-1.13.jar;C:\<path on my machine>\workspace\ESS\
tools\selenium-2.0rc2\jna.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0
rc2\json-20080701.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\juni
t-dep-4.8.1.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\nekohtml-1
.9.14.jar;C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\sac-1.3.jar;C:\U
sers\waltersj\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2-srcs.jar;
C:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\selenium-java-2.0rc2.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\serializer-2.7.1.jar;C:\Use
rs\waltersj\workspace\ESS\tools\selenium-2.0rc2\testng-6.0.1-nobsh-noguice.jar;C
:\<path on my machine>\workspace\ESS\tools\selenium-2.0rc2\xalan-2.7.1.jar;C:\Users\wa
ltersj\workspace\ESS\tools\selenium-2.0rc2\xercesImpl-2.9.1.jar;C:\Users\walters
j\workspace\ESS\tools\selenium-2.0rc2\xml-apis-1.3.04.jar;C:\<path on my machine>\work
space\ESS\build\esstestapp\tests\jar\tests.jar;C:\<path on my machine>\workspace\ESS\b
uild\esstestapp\framework\jar\framework.jar;C:\<path on my machine>\workspace\ESS;C:\P
rogram Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\apache-ant-1.8.2\lib\
ant-launcher.jar;C:\Program Files\apache-ant-1.8.2\lib\ant.jar;C:\Program Files\
apache-ant-1.8.2\lib\ant-junit.jar;C:\Program Files\apache-ant-1.8.2\lib\ant-jun
it4.jar'

任何关注这个以帮助我进行测试的人都会非常感激。

由于

詹姆斯

更新

我观察到,如果我编辑tests.classpath以删除以下2行,那么我看到的错误切换到ClassNotFound是有意义的,因为它没有对包含类的.jar文件的引用。

<fileset dir="${testsbuild}/jar" includes="**/*.jar"/>
<fileset dir="${frameworkbuild}/jar" includes="**/*.jar"/>

在tests.classpath中出现上面的2行很高兴找到它们,但是名字错误,而且这个错误我不知道如何修复。

1 个答案:

答案 0 :(得分:2)

以下 jar 命令导致的问题:

<jar destfile="${testsbuild}/jar/tests.jar" basedir="${testsbuild}" />

testsbuild 属性应设置为

<property name="testsbuild" location="C:\<path on my machine>\workspace\ESS\build"/>

说明

文件 Credentials.class 已编译为类 esstestapp.tests.Credentials 。它需要在类路径中找到文件:

esstestapp /测试/ Credentials.class