i have the following setup:
public String loadFile(String uri) throws ClientProtocolException, IOException {
StringBuilder resp = new StringBuilder();
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(uri);
HttpResponse response = client.execute(request);
}
with the Testclass looking like this:
@RunWith(JMockit.class)
public class FakeLoaderTest {
FakeLoader loader = new FakeLoader(); //class under test
@Test
public void testLoadFile(
@Mocked @Cascading final HttpClientBuilder mockBuilder,
@Capturing final HttpClient mockClient
) throws IOException, URISyntaxException{
new Expectations() {{
HttpClientBuilder.create().build(); result = mockClient;
mockClient.execute(withAny(mockget)); result = new IOException("test - test");
}};
loader.loadFile();
}
}
this gives me "Unexpected Invocation of ClosableHttpClient.execute - expected was HttpClient.execute"
HttpClientBuilder.create.build() returns a ClosableHttpClient, which implements HttpClient. I thought @Capturing took care of Mocking all classes extending the Class/interface in question?
This works fine, as expected:
@Mocked @Cascading final HttpClientBuilder mockBuilder,
@Capturing final **Closeable**HttpClient mockClient
But I want to test against the Interface, because I am not testing the HttpClient implementation that is used. In this test i dont care if someone at apache decides that ClosableHttpClient is succeeded by "ThinHttpCLient" as default implementation, as long as apache sticks to the HttpClient interface i do not want to alter this testcase. It is about testing the internal handling of an IO Exception (eg. does he Log to the right location? does he retry correctly etc.)
How do i handle this using JMockit and JUnit?
thanks in advance,
BillDoor