Coincidentally, yesterday I was struggling with testing a file upload.
I was not using S2 as web framework but I was using Commons
Fileupload, which IIRC is the default S2 upload component.

I must say that my first strategy was trying to use Spring's
MockMultipartHttpServletRequest and MockMultipartFile to simulate the
request, without any luck, so I ended up just writing the plain
request by hand:

    request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data; boundary=AaB03x");

    request.setContent(("--AaB03x\r\n" +
        "Content-Disposition: form-data; name=\"archivo\";
filename=\"texto.txt\"\r\n" +
        "Content-Type: text/plain\r\n" +
        "\r\n" +
        "This is the content of the file\n" +
        "\r\n" +
        "--AaB03x\r\n" +
        "Content-Disposition: form-data; name=\"id\"\r\n" +
        "\r\n" +
        "1\r\n" +
        "--AaB03x--\r\n").getBytes("US-ASCII"));

simulating the following form:

<form method="post" action="controller?event=cargarMaterial"
enctype="multipart/form-data">
  <input type="hidden" name="id" value="${reunion.idreunion}"/>
  <input type="file" name="archivo" size="50" />
  <input type="submit" name="btnSubmit" value="Guardar" />
</form>

Important: respect the \r\n's and the final boundary with the extra
"--" at the end of the form.

commons upload version: 1.2.2.

HTH,
Gabriel

2012/4/25 Steve Higham <st...@sjlt.co.uk>:
> I'm currently using StrutsTestCase and jUnit to component test my Actions
> within the Struts context using a Mockito mock as my Model.
>
>
>
> This is working fine. I can call request.setParamater ("paramName",
> "paramValue") from the test class and, when executed, Struts will call the
> corresponding setParamName ("paramValue") on my Action.
>
>
>
> However I now wish to test "file upload" Actions. In normal use the
> FileUploadInterceptor will call setUploadedFile (java.io.File file) on the
> action. However I can't see any methods on the StrutsTestCase request object
> which will prepare the request with an uploaded file. Has anyone got this to
> work?
>
>
>
> Steve Higham
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to