0

I am running my test from java junit test files. Its getting file fine from test folder of my project. like ABCTest is my project. inside this project I have test/testfiles folder and there is some .xlsx file. When I am getting run my test code from java class from eclipse like Run As-> Junit test , Its can read file from this folder.

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();

But for same code If I am going to run Java Test file from ant script its getting error

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

I am not getting actual reason for this.

NB : I am using spring MVC. and Junit test for this. Project is build with ant.

Exception like this

java.io.FileNotFoundException: test\testfiles\abc.xlsx (The system cannot find the path specified)
flopcoder
  • 1,195
  • 12
  • 25

1 Answers1

0

So when you look at the exception your getting you can see that the file on the path test\testfiles\abc.xlsx could not be found. This is pretty logical because it doesn't specify the full path. You could for example try to copy the path and paste it in your file explorer. The solution for this is would be getting the file from your working directory. You can do this with the code in the example below:

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

But a better solution would be using the resource directories of your project. The file should be in the test resource directory and in the main resource directory for this to work. If you'd do that you could use:

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

Note: I use File.separator so it will work fine on windows as linux bases operationg systems. You can read more about that here

Casper
  • 293
  • 2
  • 22
  • Please read my question carefully. I didn't mean this. Its a spring MVC application. – flopcoder Sep 10 '18 at 17:41
  • The exception you are getting should be fixed with this answer. The path that you are specifying is no real path on your machine. That your project has Spring MVC as dependency doesn't have anything to do with this for how I am understanding your question now. To be sure; the problem is the exception you are getting what you described in your question right? – Casper Sep 10 '18 at 18:58