Search code examples
javaapache-commons-httpclient

Spoofing postMethod (apache in java)?


Im fairly new to this so please dont slam me down.

I'm trying to mock a Postmethod so that I can set the status it returns. For instance I want to set my PostMethod to 200.

The reason for doing this is I am trying to mock a computer without an internet connection

Thanks in advance


Solution

  • What mocking library are you using? The syntax will vary.

    In Mockito, for example, you might do the following:

    final PostMethod mockPostMethod = Mockito.mock(PostMethod.class);
    when(mockPostMethod.execute(Mockito.any(HttpState.class),
        Mockito.any(HttpConnection.class))).thenReturn(200);
    

    And of course you could use a static import of the Mockito class to avoid the extra qualified references.