I am trying to write Junit for a simple method that takes filename as input and returns the byte array as output. The difficulty I am facing is that the file will not be available while running Junit. So, how can I test this method? I see two options:
- somehow make the file available for Junit (I am not sure if this is possible).
- Mock/stub the behavior.
I am pasting the code of the method below:
public byte[] readFileAndReturnBytes(String filePath) throws IOException {
InputStream is = null;
File file = null;
byte[] fileBytes = null;
try
{
file = new File(filePath);
is = new FileInputStream(file);
fileBytes = IOUtils.toByteArray(is);
}
catch(IOException e){
throw e;
}
finally{
if(is != null)
{
is.close();
file = null;
}
}
return fileBytes;
}
I am using Mockito for mocking. Does anyone have any ideas?