I have a piece of code that generates some file an I need to cover this code with unit-tests. With a sample file I may check my code whether the file was generated correctly. Maven creates src/test/resources directory automatically, there is no better place to put the sample file.
If the file has a path /src/test/resources/tables/sample.xml we can read them in a following way
Don't need to compare files line-by-line, just use commons-io util
Finally, three lines of code below can be applied for unit tests (and not only for tests) to check for files equality:
If the file has a path /src/test/resources/tables/sample.xml we can read them in a following way
URL url = this.getClass().getResource("/tables/sample.xml"); File sampleFile = new File(url.getFile());
Don't need to compare files line-by-line, just use commons-io util
FileUtils.contentEquals(file1, file2);
Finally, three lines of code below can be applied for unit tests (and not only for tests) to check for files equality:
private boolean isFileDataEqual(File target) throws IOException { URL url = this.getClass().getResource("/tables/sample.xml"); File sampleFile = new File(url.getFile()); return FileUtils.contentEquals(target, sampleFile); }
No comments:
Post a Comment