If you are not testing system integration, IE launches a full stack with external servers, etc., then using bullying tools is what you are looking for. They allow you to record the behavior of the library you are working with. This will allow you to run library code that does not require verification.
Using something like mockito, or if necessary PowerMock , you can tell the library to throw exceptions when the method you call is called.
Example:
import static org.mockito.Mockito.mock import static org.mockito.Mockito.when ... @Test(expected=HttpException.class) public void invockationThrowsHttpException() { ... HttpClient httpClient = mock(HttpClient.class) when(httpClient).executeMethod(args...).thenThrow(HttpException...) underTest.invokeCodeUnderTest(args...) ... }
The above example assumes JUnit4. The Mockito website has a pretty good tutorial. PowerMock is used if you need to scoff at the fact that mockito is not mocking (for example, static or final methods).
As an aside, I noticed that the Apache HTTP Client is at the end of life and that they recommend switching to the Apache HTTP components instead. If your project is still in its early stages, you can now switch it.
Tomas malmsten
source share