Re: [mockito] Mocking HTTP @POST request in MultipartEntity format

509 views
Skip to first unread message

Eric Lefevre-Ardant

unread,
Mar 12, 2013, 4:59:30 PM3/12/13
to moc...@googlegroups.com
You say "I am trying to mock my method which sends binary file", but I assume you mean "I am trying to *test* my method which sends binary file".

I see that you are using magic classes such as DefaultHttpClient who actually do the sending. I assume this is what is causing your trouble.

I think you need to provide mocked instances of those. For this, there aren't many techniques.
  • provide a builder method (createHttpClient()) that you override in your test,
  • provide a factory class (HttpClientFactory) to the constructor of the class that contains the sendPostRequest() method, then, in your test, call the constructor of that class with an appropriate version of the factory that returns mocks.
Assuming you do not want your test to touch any of your system (such as the file system), you might need to do that for several other classes, such as File.

HTH,

Eric


On 12 March 2013 14:25, roman janulek <roman....@gmail.com> wrote:
Hello everyone,

I quite new to mockito and I am trying to mock my method which sends binary file and couple of strings as @POST request. The request is send as MultipartEntity. I am really not sure how to do that. I found couple of examples but none of them was useful.

Here is my method I want to:

    public void sendPostRequest(Exchange exchange) throws IOException {
        String pathToUploader = "https://" + exchange.getProperty("HOST").toString() + ":"
                + exchange.getProperty("PORT").toString()
                + exchange.getProperty("UPLOAD_API").toString();

        String filename = exchange.getProperty("PATH_TO_FILE").toString();
        String session_id = exchange.getProperty("SESSION_ID").toString();
        String action = "A";
        String output = "json";
        File myfile = new File(filename);
       
        if(myfile.exists()){

            HttpClient client = new DefaultHttpClient();
            client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpPost post = new HttpPost(pathToUploader);
   
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
   
            // For File parameters
            entity.addPart("myfile", new FileBody(((File) myfile), "application/octet-stream"));
   
            // For usual String parameters
            entity.addPart("session_id", new StringBody(session_id, "text/plain", Charset.forName("UTF-8")));
            entity.addPart("action", new StringBody(action, "text/plain", Charset.forName("UTF-8")));
            entity.addPart("output", new StringBody(output, "text/plain", Charset.forName("UTF-8")));
   
            post.setEntity(entity);
   
            String response = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8");
            exchange.getOut().setBody(response);
   
            client.getConnectionManager().shutdown();
        }else{
            logger.error("File does not exist!!!");
            throw new FileNotFoundException();       
        }

    }

Thanks for any help

-Br, Roman

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages