How to test API contracts when there is a dependency on data from an outside source

I’m after some advice regarding a project I am currently working on. There is a front-end, similar to what Amazon is like in that sales are fed through the site(there is no API for doing this). However, the requirement I have is to test the microservices that sit behind the UI which go off to various warehouse management systems.

The issue I have is that in order to test each of these services it’s dependent on data being passed through from the UI. Obviously it’s fine to do this, but each of these tests will take a while to run; what I’m interested in is more of the integration behind each of these micro-services, rather than having to worry too much about the API. Does anyone have any advice about how I could go about this?

1 Like

Hello Andrew!

I think it’s a healthy idea to separate UI evaluations from API behavior! If you have the API endpoints (these look like URLs) and the format of the payload (usually a JSON format), then you might consider Postman for your evaluations.

Joe

1 Like

what I’m interested in is more of the integration behind each of these micro-services, rather than having to worry too much about the API

This reads to me like you just want a test to verify that the UI makes the proper API call, and that you don’t care about the underlying API? i.e. that if they click on the “update cart” button or something, the payload should be something like:

{"itemId": "foo", "quantity":1, "cartId": "bar"}

You want your test to just verify the UI does that, and that seems like it should be doable by mocking the micro-services, and just have dummy responses come back quickly and eliminate the timing issues.

I do think this is contrary to “the requirement I have is to test the microservices” - i.e. you’re just going to be testing the contracts, and not the actual microservices

1 Like

Thanks for the response Ernie. Could I potentially build a series of classes which contain the payload which the API is expecting, in order to mock the API?

You can do that, yes. If it’s being used across multiple tests then that is better practice.

If I want to create several different responses, what I normally do is define an object for the response e.g.

public class ApiResponse
{ 
  public string Field1;
  public int Field2 ;
}

We use RestSharp in C# which will then serialise the response into Json. But since you’re mocking the response then you can just do something like: https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net

1 Like

Thanks Ali. I’ve actually managed to do something like that using RestSharp in that I’ve build a series of classes which contain the data to be passed in the API. And each test I have can then pass across different sub-sets of data of my choosing

1 Like

Sure. Another option instead of mocking in code could be to build a mock server using a tool like https://github.com/yahoo/fake-server, https://github.com/pretenders/pretenders, https://github.com/typicode/json-server, etc or a service like https://www.mocky.io/ if the application is written in a way you can easily change the host it makes its API calls to.

I haven’t used it, but Fake-server looks pretty flexible in that you can just make API calls to modify the responses.