Search code examples
javaspringintegration-testinghtmlunit

Test POST method to send the file content


I have a method in Controller which takes the MultipartFile(Spring class )object as and stores it. I have to do an integration test. So when the user logs in , it should be able to upload a file. I am using htmlUnit Webresponse class to send the request to controller @localhost. how can i add the Multipartfile object to Webresponse or is there some other way to test it ?

Thanks Harshit


Solution

  • I found the solution

    MultipartPostMethod method =
            new MultipartPostMethod( "http://localhost:8080/sendfile" );
                       method.addParameter("mediaFile", multifile );
       HttpClient client1 = new HttpClient( );
    // Execute and print response
        client1.executeMethod( method );
        String response = method.getResponseBodyAsString( );
        System.out.println( response );
        method.releaseConnection( );
        return response;
    

    The class is part of apache-httpclient jar