从ant脚本运行Junit Test脚本时文件导入测试失败

时间:2018-09-10 06:40:44

标签: java spring-mvc ant junit5

我有一些JUnit测试代码,它将读取文件并将其导入到我的项目中,然后基于一些我确定测试成功与否的参数。当我从Eclipse运行它时,它的工作正常。但是当我要从某个ant脚本运行它时,它会像这样得到Exception。

org.xml.sax.SAXException: Premature end of file.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
    at .service.data.excel.poi.HandlerServicePoiXssfEventImpl$1.fatalError
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)

我的测试代码是这样

fileReference = "test/testfiles/namefile.xlsx";
        data = communicationService.readFileFromDisc(fileReference, null);
        // Now, let's try to import tender file.
        result = dataHandlerService.importFile(
                new ByteArrayInputStream(data));

        log.info("+++ Result is: " + result);
        //then some assert function

和这样的蚂蚁脚本

<target name="prepare" depends="resolve">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.test.classes.dir}" />
        <mkdir dir="${build.test.reports.dir}" />

        <path id="test.cp">
            <fileset dir="${lib.test.dir}">
                <include name="junit-4.8.2.jar" />
                <include name="dozer-*.jar" />
            </fileset>
            <fileset dir="${lib.dir}">
                <include name="*.jar" />
            </fileset>
            <fileset dir="${lib.apache.dir}">
                <include name="*.jar" />
            </fileset>
        </path>
    </target>

    <target name="tests.compile" depends="prepare">
        <javac destdir="${build.test.classes.dir}" excludes="/backend/security/**/*.*" debug="true" optimize="true" deprecation="false" failonerror="true">
            <src path="${src.test.dir}" />
            <src path="${src.dir}" />

            <classpath>
                <path refid="test.cp" />
            </classpath>
        </javac>
        <copy file="test/src/dozer.properties" todir="${build.test.classes.dir}" />
        <copy file="test/src/test.log4j.properties" todir="${build.test.classes.dir}" />
        <copy todir="${build.test.classes.dir}/testApplicationContexts">
            <fileset dir="${src.test.dir}/testApplicationContexts" />
        </copy>


        <copy todir="${build.test.classes.dir}/test/testfiles">
                <fileset dir="${basedir}/test/testfiles" />
        </copy>
    </target>

    <target name="tests.run" depends="tests.compile">
        <junit fork="yes" forkmode="once" haltonfailure="no" showoutput="no" printsummary="yes" maxmemory="256m">
            <jvmarg value="-Dlog4j.configuration=test.log4j.properties"/>

            <formatter type="xml" />
            <classpath>
                <pathelement location="${build.test.classes.dir}" />
                <path refid="test.cp" />
            </classpath>
            <batchtest todir="${build.test.reports.dir}">
                <fileset dir="${src.test.dir}">
                     <include name="**/ImportTest.java" /> 
                </fileset>
            </batchtest>
        </junit>

如果我是从ant脚本运行的,则测试代码不会成功,但是如果我从Eclipse以 Run As-> Junit Test 的身份运行,则代码不会成功。我的问题在哪里?从ant脚本运行时是否无法正确加载完整文件??

0 个答案:

没有答案