2

I want to write a unit test to test creating .zip file from two .doc files. BU I take an error: Error creating zip file: java.io.FileNotFoundException: D:\file1.txt (The system cannot find the file specified)

My code is here:

@Test
public void testIsZipped() {

    String actualValue1 = "D:/file1.txt";
    String actualValue2 = "D:/file2.txt";

    String zipFile = "D:/file.zip";

    String[] srcFiles = { actualValue1, actualValue2 };

    try {

        // create byte buffer

        byte[] buffer = new byte[1024];

        FileOutputStream fos = new FileOutputStream(zipFile);
        zos = new ZipOutputStream(fos);

        for (int i = 0; i < srcFiles.length; i++) {

            File srcFile = new File(srcFiles[i]);

            FileInputStream fis = new FileInputStream(srcFile);

            // begin writing a new ZIP entry, positions the stream to the
            // start of the entry data

            zos.putNextEntry(new ZipEntry(srcFile.getName()));

            int length;

            while ((length = fis.read(buffer)) > 0) {

                zos.write(buffer, 0, length);
            }

            zos.closeEntry();

            // close the InputStream

            fis.close();
        }

        // close the ZipOutputStream

        zos.close();

    }

    catch (IOException ioe) {

        System.out.println("Error creating zip file: " + ioe);
    }

    String result = zos.toString();

    assertEquals("D:/file.zip", result);
}

Can I get name of zip file from zos to test, How to understand to pass the test? Can anybody help me to solve this error? Thank you.

eponymous
  • 2,090
  • 5
  • 23
  • 29
  • My guess is that the file is either not there or the path is wrong (Windows uses `\` to separate path, which you have to escape. – hotzst Oct 06 '15 at 14:24
  • Does `D:\file1.txt` exist? Do you have read rights to it? Possibly consider trying `D:\\file1.txt` – CollinD Oct 06 '15 at 14:24
  • I created file1.txt and file2.txt files in D directory. Now there is no error but I did not pass the test. I tried these to define directory : "D:\\file1.txt", "D:\file1.txt" and "D:/file1.txt". – eponymous Oct 06 '15 at 14:30

1 Answers1

0

First, are your files created in a previous test method? If yes consider that junit tests do not execute in the order you defined your test methods, have a look at this:
How to run test methods in specific order in JUnit4?

Second, you could add a debugging line:

File srcFile = new File(srcFiles[i]);
System.out.append(srcFile+ ": " + srcFile.exists() + " " + srcFile.canRead());

After you solve the exception you will run into this problem, the test will fail:

String result = zos.toString();

assertEquals("D:/file.zip", result);

zos.toString() will return something like: "java.util.zip.ZipOutputStream@1ae369b7" which will not be equal to "D:/file.zip".

String zipFile = "D:/file.zip";
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
System.out.println(zos.toString());
Community
  • 1
  • 1
flavio.donze
  • 7,432
  • 9
  • 58
  • 91