Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NoClassDefFoundError通过Ant运行测试但在Tomcat中部署时工作_Java_Tomcat_Junit_Ant - Fatal编程技术网

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

Java NoClassDefFoundError通过Ant运行测试但在Tomcat中部署时工作,java,tomcat,junit,ant,Java,Tomcat,Junit,Ant,当我通过ant运行时,我得到NoClassDefFoundError,但当我在tomcat中部署应用程序时,它正在工作 <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"/>

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

<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>
我使用

ant test 
<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>
例外情况

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)
<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>
java.lang.NoClassDefFoundError:org/apache/commons/dbcp2/BasicDataSource
位于com.x.ws.repo.DatabaseConnection。(来源未知)
位于com.x.ws.repo.DatabaseConnection.getSynchronizedInstance(未知源)
位于com.x.ws.repo.DatabaseConnection.getInstance(未知源)
在com.x.ws.repo.Foo(未知来源)
位于com.x.ws.repo.FooTest.setUp(未知来源)
蚂蚁脚本

<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>

<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>

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

<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>