views:

282

answers:

3

Does anyone have suggestions for a java mock File object? I Am using a thirdparty class which need to get a java.io.File object as argument. I receive the data for this file in a stream over a webservice (also one of their products).

One solution is to write all this data to a file and offer this to the class. This is a solution I don't like: it takes away the advantage of using the webservice in stead of just downloading the file.

Quicker and more efficient would be to put this data from memory in a Mock File and offer this Mock File to the thirdparty class.

It would probably have to be a MockFile extending the java.io.File and overriding all the functions that do actual interfacing with the file on the hard disk.

I know the thirdparty should have used a stream as an input argument in stead of a file. However, this is beyond my influence.

+1  A: 

Does the tested class only query the mock File's name, attributes etc., or does it actually attempt to open the file?

In the former case, you can easily create your mock using e.g. EasyMock or an equivalent mocking framework.

The latter case is more tricky, and I am afraid if the input stream is created internally by the class, you have no choice other than actually creating a real test file on the HD.

Péter Török
It actually wants get (read) the data out of the file. So I Am looking for a mock file the overrides read as well...And read from the data in the memory (in the mock file)I know it's more tricky, but hopefully somebode already has done this...
michel.iamit
@michel, the problem is, `File` is only "an abstract representation of file and directory pathnames" [(from the Javadoc)](http://java.sun.com/javase/6/docs/api/java/io/File.html). So you can't read directly from it - you need to open a [`FileInputStream`](http://java.sun.com/javase/6/docs/api/java/io/FileInputStream.html) (or [`FileReader`](http://java.sun.com/javase/6/docs/api/java/io/FileReader.html)) for that.
Péter Török
I know, so need to return a file inputstream from this mock file object. Thanx for helping and thinking along, The suggestion below seems to offer the searched solution....
michel.iamit
Part of what you suggested leeds to my end conclusion, what I want cannot be done. Need to actually make a file. The wrapper can only be used to auto generate a temp. file. For what I want, the thirdparty must add an option to pass an inputstream in stead of the File.
michel.iamit
It may not be the best solution in this case, but you *can* mock an input stream (and just about anything else) with the JMockit mocking library. Even if said stream is created inside the third-party class.
Rogerio
A: 

Have you considered using Commons VFS?

al nik
As marked above, this package seem to offer the solution I Am looking for... will let you know whether this is usefull.
michel.iamit
+2  A: 

This is just a suggestion based on my understanding of your question. I believe, you must be doing something like this,

public void doSomething(){
      //Pre processing
       Object result=new ThirdPartyCode().actualMethod(file);
     //Post processing
}

Mock objects make more sense from an unit testing perspective. Your objective is not to unit test the third party library function.Whereas it is to unit test doSomething() method. So probably you can create a wrapper around the third party function.Maybe something like this,

public class Wrapper implements MyWrapper{

   public Object invokeThirdPartyFunction(File file){
      new ThirdPartyCode().actualMethod(file);
   }
}

Now you can create a mock wrapper(implementing the same interface) and use this mock wrapper for all your junit cases.

chedine
Thanx, this in combination with the answer about common VFS will probably do the trick. (need something like the common VFS to create a virtual file while not actually creating the file on harddisk)
michel.iamit
Welcome and good luck.
chedine