Hunsu
Hunsu

Reputation: 3381

EasyMock is calling the real method

I have this test case:

TestClient

@RunWith(EasyMockRunner.class)
public class TestClient extends EasyMockSupport {

    @TestSubject
    private final IClient client = new Client();

    @Mock
    private HttpClient httpClient;

    @Mock
    private HttpUriRequest request;
    @Mock
    private HttpResponse response;

    @Test
    public void testExecute() throws ClientProtocolException, IOException {
        expect(httpClient.execute(request)).andReturn(response);
        replayAll();
        httpClient.execute(request);
        client.execute(request);
        verifyAll();
    }
}

Client

public class Client implements IClient {

    private final HttpClient httpClient;

    public Client() {
        httpClient = createDefaultClient();
    }

    private HttpClient createDefaultClient() {
        return HttpClientBuilder.create()
                .build();
    }

    @Override
    public HttpResponse execute(final HttpUriRequest request)
            throws IOException {
        return httpClient.execute(request);
    }

}

When I run it I get this error:

Unexpected method call HttpUriRequest.getURI()

Normally the created will not call this method.

Why I get this error? Why it require me to define a result for getURI method?

Upvotes: 0

Views: 2639

Answers (3)

dimo414
dimo414

Reputation: 48804

You must remove final from

private final HttpClient httpClient;

Otherwise EasyMock cannot overwrite your httpClient with its own mocked instance. Of course better still would be to directly use dependency injection, but that's tangential to your actual issue.

You can use reflection as a workaround; but dear me no...


Original answer:

Posting a full stack trace, and relevant parts of your Client class would help us debug this with you. You mention that:

in client.execute(request) I do httpClient.execute(request).

But (at least in the code you've posted) nowhere does client get a reference to the mocked httpClient instance. Does your Client class perhaps construct it's own HttpClient instance, and make requests against that instead of your mock?

If so, your Client should follow the dependency injection pattern and pass in an HttpClient instance at construction, rather than construct one internally.

Another possibility is that your Client.execute() method is calling request.getURI() at some point, in which case you simply need to expect() a call to request.getURI().

Upvotes: 2

eee
eee

Reputation: 3458

I suspect the problem is the call of httpClient.execute(request);. Try to remove it. You already setup a expectation for the httpClient via expect(httpClient.execute(request)).andReturn(response);.

@Test
public void testExecute() throws ClientProtocolException, IOException {
    expect(httpClient.execute(request)).andReturn(response);
    replayAll();
    client.execute(request);
    verifyAll();
}

Upvotes: 0

Brett Walker
Brett Walker

Reputation: 3576

I suspect that inside of the method client.execute(request) there is a request.getURI() statement (or buried deeply in called methods). Easy Mock needs to know the response to give when this method is called.

Upvotes: 0

Related Questions