shikarishambu
shikarishambu

Reputation: 2269

Automated testing of file generation and validation of file contents

We have a java web application that generates files (zip) and drops it to file system. Is there a way to bring this within the realm of test automation - i.e. validate that a) file/packet got generated b) the packet contents match and c) individual files have necessary data. the contents of the packet are typically pdf, txt and xml files

Are there any tools/ methods to effectively test this

Upvotes: 0

Views: 770

Answers (3)

ekostadinov
ekostadinov

Reputation: 6940

I have similar tasks like yours, but in XLSX format. Maybe my approach can help you:

  • about a) you can use the code exception_handling logic (expected behavior, error codes)
  • about b) and c) you can combine your code to validate a checksum (see here and here) of the files

In my case I have report_generator which prints such results as execution_flow, regression etc. So as bottom line - yes this definitely must be put into the realm of test automation

  • Cheers

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53809

You can use TemporaryFolder from Junit:

The TemporaryFolder Rule allows creation of files and folders that are guaranteed to be deleted when the test method finishes (whether it passes or fails)

public class HasTempFolder {
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void testUsingTempFolder() throws IOException {
        File createdFile= folder.newFile("myfile.txt");
        File createdFolder= folder.newFolder("subfolder");
        // ...
    }
}

For checking file contents, FileUtils from Apache Commons IO has a lot of very useful methods.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272457

The TDD way would be to inject mock objects that handle the actual file writing.

In practice, you could just use JUnit to automatically create temporary directories for the lifespan of a test-case (with a TemporaryFolder rule; see http://junit.org/javadoc/4.9/org/junit/rules/TemporaryFolder.html).

Upvotes: 1

Related Questions