• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wait for file to be completely written

 
Alexander D´┐Żubler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have the following question:

in one of our JUnit tests we use a printer driver to print to a file. However, we have to wait for this file to be completely written before we can continue the test-process. How can I ensure that the target file has been written completely?

Thanks a lot,
Alex
 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A unit test should not test whether a file is printed or not. A unit test should not depend external devices, machines, DBs etc.. and should run anywhere quickly .
Please try to mock printer or printer driver in your unit tests.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That fits Michael Feathers's definitions in It's not a unit test if ... and sure enough if you're testing the document generation you don't really need the printer driver. Let us know if that's the case and we can discuss mock objects.

But if the goal is to test the driver or if you can't change the code that calls it, you can't very well mock the driver out. Maybe we just have to call it something other than "unit" test.

So, I guess the driver runs on another thread, maybe even the OS print spooler? If you run the thread you can join() it to know when it's done. If not, the crude way to tell when it's probably done is to check the file every second or two and see when it stops getting bigger. It would be cool to find an API into the print queue or something that you could query or register for events but that could wind up taking more code than the class youre trying to test.

What have you tried so far?
 
Per Wramdemark
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing the exact problem. I want to write a unit test but understand that I shouldn't include writing to disc in my unit testing. But still I want to test my FileHandler.java that does the actual writing. Does there exists any memory File handling that could be used instead? Any pointer or tips?

thanks,

Per
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic