I have a piece of code in Java that zips the contents of a directory into a zip 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.