通过Ant运行测试时的NoClassDefFoundError,但是在Tomcat中部署时工作

时间:2016-08-09 12:56:58

标签: java tomcat junit ant

当我通过ant运行时,我得到NoClassDefFoundError但是当我在tomcat中部署应用程序时它正在工作。

我使用

调用它
ant test 

异常

java.lang.NoClassDefFoundError: org/apache/commons/dbcp2/BasicDataSource
at com.x.ws.repo.DatabaseConnection.<init>(Unknown Source)
at com.x.ws.repo.DatabaseConnection.getSynchronizedInstance(Unknown Source)
at com.x.ws.repo.DatabaseConnection.getInstance(Unknown Source)
at com.x.ws.repo.Foo.<init>(Unknown Source)
at com.x.ws.repo.FooTest.setUp(Unknown Source)

Ant脚本

                                                           

<path id="classpath.test">
    <pathelement location="WebContent/WEB-INF/lib/junit-4.11.jar"/>
    <pathelement location="WebContent/WEB-INF/lib/hamcrest-all-1.3.jar"/>
    <pathelement location="WebContent/WEB-INF/lib/java-json.jar"/>
    <pathelement location="${main.build.dir}"/>
</path>

<target name="test-compile" depends="compile">
    <javac srcdir="${test.src.dir}" destdir="${test.build.dir}">
        <classpath refid="classpath.test"/>
        <classpath refid="classpath"/>
    </javac>
</target>

<target name="test" depends="test-compile">
    <junit printsummary="yes" haltonfailure="no">
        <classpath refid="classpath.test" />
        <classpath location="${test.build.dir}" />

        <formatter type="xml" />
        <formatter type="plain" />
        <batchtest fork="yes" todir="${reports.src.dir}">
            <fileset dir="${test.src.dir}">
                <include name="**/*Test*.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

1 个答案:

答案 0 :(得分:2)

我想DBCP目前打包在你的$ {CATALINA_HOME} / lib中,即Tomcat的lib目录。由FooTest运行的代码中定义的数据源必须定义为BasicDataSource,因此您应在commons-dbcp.jar中为classpath.test添加另一个条目。