【问题标题】:File not found If test run using ant script找不到文件如果使用 ant 脚本进行测试
【发布时间】:2018-09-10 10:53:33
【问题描述】:

我正在从 java junit 测试文件运行我的测试。它从我的项目的测试文件夹中获取文件。像 ABCTest 是我的项目。在这个项目中,我有 test/testfiles 文件夹,并且有一些 .xlsx 文件。当我像 Run As-> Junit test 这样从 eclipse 的 java 类运行我的测试代码时,它可以从这个文件夹中读取文件。

fileNameWithPath="test/testfiles/abc.xlsx"          
out = new ByteArrayOutputStream();
            fis = new FileInputStream(fileNameWithPath);
            bis = new BufferedInputStream(fis);

            while ((c = bis.read()) != -1) {
                out.write(c);
            }

            fileData = out.toByteArray();

但对于相同的代码,如果我要从 ant 脚本运行 Java 测试文件,则会出现错误

<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="**/FileReadTest.java" /> 
                </fileset>
            </batchtest>
        </junit>

我没有得到真正的原因。

注意:我正在使用 Spring MVC。和 Junit 测试。项目是用 ant 构建的。

这样的异常

java.io.FileNotFoundException: test\testfiles\abc.xlsx (The system cannot find the path specified)

【问题讨论】:

  • 您能否与您获得的堆栈跟踪共享异常?
  • @Casper 已更新。

标签: java spring spring-mvc ant


【解决方案1】:

因此,当您查看异常时,您会看到路径 test\testfiles\abc.xlsx 上的文件找不到。这是非常合乎逻辑的,因为它没有指定完整路径。例如,您可以尝试复制路径并将其粘贴到文件资源管理器中。解决方案是从您的工作目录中获取文件。您可以使用以下示例中的代码执行此操作:

File file = System.getProperty("user.dir")) + File.separator "test" + File.separator + "testfiles" + File.separator + "abc.xslx";

但更好的解决方案是使用项目的资源目录。该文件应位于测试资源目录和主资源目录中才能正常工作。如果你愿意,你可以使用:

YourClassName.class.getResource("path/within/resource/directory")

注意: 我使用 File.separator,所以它可以在 Windows 上正常工作,因为它是基于 linux 的操作系统。你可以阅读更多关于here

【讨论】:

  • 请仔细阅读我的问题。我不是这个意思。它是一个 Spring MVC 应用程序。
  • 你得到的异常应该用这个答案来解决。您指定的路径不是您机器上的真实路径。您的项目将 Spring MVC 作为依赖项与我现在如何理解您的问题没有任何关系。为了确定;问题是你得到你在问题中描述的例外吗?
猜你喜欢
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 2017-12-18
  • 2011-06-01
  • 1970-01-01
  • 2011-11-16
相关资源
最近更新 更多