Search code examples
javaunit-testingstruts2struts2-junit-plugin

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


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?


Solution

  • 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();