Reputation: 784
In integration testing, I use a real remote server to consume REST APIs. What's the simplest way to provide those responses within a unit test w/o depending on external entity.
One possible approach is to build
public class TestHttpResponse implements org.apache.http.client.methods.CloseableHttpResponse
override
@Override
public StatusLine getStatusLine() {
return new StatusLine() {
@Override
public ProtocolVersion getProtocolVersion() {
return null;
}
@Override
public int getStatusCode() {
return statusCode;
}
@Override
public String getReasonPhrase() {
return reasonPhrase;
}
};
}
...
Is there a simpler and better way for mocking REST API response payloads?
Upvotes: 2
Views: 1555
Reputation: 11
I'm a fan of Mockito for this purpose:
http://www.vogella.com/tutorials/Mockito/article.html#testing-with-mock-objects
The nice part about Mockito is how you can control it's behavior dynamically.
Upvotes: 1