Rockoder
Rockoder

Reputation: 784

Mock Rest Server Response for Unit Testing

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

Answers (1)

Sean Yunt
Sean Yunt

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

Related Questions