2

I'd like to write an integration for a Java program that takes an input file from source directory, copies it to staging, writes the result of processing to output directory and then removes the temporary files from staging. It's easy enough to check the output is ok, but is there an easy way to check the staging area is being used correctly? 2 things come to mind: monitoring the folder for file system events (any nice wrappers for that?), or some advanced permissions game (to allow create but not delete). Any suggestions?

Thanks a lot!

N.B. Java 6 on Windows...

seminolas
  • 407
  • 8
  • 15

1 Answers1

0

Assuming you're dead set on doing this, I think the easiest approach is just going to be checking that the file is in the staging folder after its supposed to have been copied and then checking that it's not there after it's supposed to have been deleted.

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { /* do something */}

How do I check if a file exists in Java?

Community
  • 1
  • 1
shieldstroy
  • 1,307
  • 1
  • 10
  • 24
  • The problem here is that copying, processing and cleanup happen within the method under test. So before the method runs, staging will be empty; and so it will be after the method returns. – seminolas Jun 29 '15 at 15:04
  • I see. You might consider breaking some of these components into separate functions/classes and testing them each individually then. For example, you could create a class whose entire responsibility is FileIO. The method you are testing just calls, "myFileIO.copyFile(from, to)". Then when you are writing a test you have a lot more freedom. You can use a mock MyFileIO object without worrying about whether or not the file actually made it to the directory or not, but you can focus merely on the logical execution of your program. Then separately you can write tests for MyFileIO. – shieldstroy Jun 29 '15 at 15:29
  • Of course that is the *right* answer. However, I'm stepping into production code with 0 tests, so I kinda need some end-to-end test *before* I'm allowed to refactor... – seminolas Jun 29 '15 at 15:40
  • Ahh I see. In that case... I personally would just do it and write the test to prove that what I refactored still works. I know that might not be an option though. – shieldstroy Jun 29 '15 at 16:14