How to test a file upload in a struts unit test?

870 Views Asked by At

In Struts 2, I can use request.setParameter() for testing normal form submissions. But, how can one test uploading a file? Is there an equivalent of request.setParameter() which lets you pass a file as the parameter value?

I also have a setFile() method on my action which I could use to set the file before I call actionProxy.execute(), but, would doing that not reset the value of my file if there's no file on the request?

1

There are 1 best solutions below

0
On BEST ANSWER

The following code is working for me:

    File newFile = new File("/path/to/file.png");
    ActionProxy proxy = getActionProxy("/uploadAction");        
    UploadAction action = (UploadAction) proxy.getAction();                

    action.setMyUploadFile( newFile );
    String result = proxy.execute();