Testing feign clients

Do you know netflix feign? No? You should!

In short, feign is a quick and easy way to write a client for APIs. You can even use it to produce the client for your API easily!

What I like about it is:

  • compile time check

  • really easy to mock

If mocking feign clients is easy, testing the logic written in annotations is everything but!

To check if you are parsing the request/response properly the only way it firing a real request. Well, that doesn’t seem to be a good path to unit (or even integration) test remote services. Any IO change will affect test stability.

That is why I create feign-mock.

With feign-mock you can using pre-loaded json strings or streams as content for your responses. It also allow you to verify mock invokation and feign-mock will hit your annotations to make sure everything works.

Example
  private GitHub github;
  private MockClient mockClient;

  @Before
  public void setup() throws IOException {
    mockClient = new MockClient()
        .noContent(HttpMethod.PATCH, "/repos/velo/feign-mock/contributors");

    github = Feign.builder()
        .decoder(new GsonDecoder())
        .client(mockClient)
        .target(new MockTarget<>(GitHub.class));
  }

  @Test
  public void missHttpMethod() {
    List<Contributor> result = github.patchContributors("velo", "feign-mock");
    assertThat(result, nullValue());
    mockClient.verifyOne(HttpMethod.PATCH, "/repos/velo/feign-mock/contributors");
  }

This simple test returns no content and verify if the url was trully invoked.

On mock client, you can include all urls and methods you wanna mock.

For a more compreensive example take a look at MockClientTest.

comments powered by Disqus