JUnit for a directory zipper
I have a piece of code in Java that zips the contents of a directory into a z开发者_C百科ip file.
The signature of the method is as follows:
/**
* Zips the contents of the directory into the zip file.
* @param directory the directory to zip
* @param zipFilename the file name to zip into
*/
public static void doZIP(String directory, String zipFileName)
{
//do the zipping
}
Now I have to write a JUnit test case for the above method. What exactly should I test, and how?
NOTE : I am not looking for answers on how to write a JUnit test case.
Thanks in advance.
Create a temporary directory and write some files into the directory. Then call the method with the temp directory as argument and a given zip file name. Test that the zip file exists. Then test that you can open it with ZipFile and find the expected zip entries in the zip file.
The method doesn't specify its contract very well. For example, it doesn't say what it does if the directory doesn't exist, or if it's impossible to write the zip file, so it's hard to test these aspects. But normally, it's the developer of a method which writes the unit test for it, because he knows what the corner-cases are.
Ideally your test would be a success if:
1) You can create a valid zip file that is accessable by applications like 7-Zip or Winzip
2) When you unzip the contents you get back exactly what you zipped
I'm not sure if you could facilitate these with JUnit tests unless you've built in decompressing functionality to your application, in that case it may be easier to just run the section of your code and do the tests manually.
If you have function to decompress the zip after its created then use something like MD5 hashes to compare the old/new contents for matches.
精彩评论