【发布时间】: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