avi
avi

Reputation: 195

Spring integration testing for REST call of some other Service

I've been searching it on the net , but most of the examples i found have the returning logic of rest call within the same project , but what if you want to test a rest call of some other service which you are using it in your project(calling a REST api from a REST API)

is there any way to integrate this . Integration testing for a REST call which is of some external service

can the normal Integration testing approach could work in this case.

Upvotes: 0

Views: 662

Answers (1)

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11149

Have you considered using Spring Cloud Contract (http://cloud.spring.io/spring-cloud-contract/) ? It's a project design specifically for that purpose.

You have the producer of the API and its consumers. The idea of Spring Cloud Contract and Consumer Driven Contract approach is such, that the consumers suggest how the API of the producer should look like. They can prototype the API without writing any production code on the producer side. The prototyping takes place in a form of a "contract". It can be a Groovy or a YAML file (you can of course extend the framework). Processing of the contract results in a creation of a WireMock stub that the consumers can leverage in their integration tests. In other words, it's as if the producers would prepare a small, fake implementation of their code for testing perspective. So the consumers can run their integration tests against a stub of the producer side. The stub was generated from the contract. Let's say that the consumer X wants to use an API in such a way that if a GET request is sent to /foo it will respond with text bar. Then a stub that responds with a bar text, when hit at the /foo endpoint will be generated.

Now, the producers reuse the same contracts to generate tests to verify if their API meets the requirement of what's there in the contract. Remember the GET @ /foo will respond with bar example? If the producer tries to build its project and doesn't have such an endpoint, its build will be broken. The Spring Cloud Contract framework generates the tests that assert whether the API is working the way it should. Only after the producer fixes the missing implementation will the build pass.

This is the consumer driven contract approach. You can also do the producer driven approach where the producer of the API just defines the contracts without communicating with the consumers how exactly each of them is using the API.

Valuables links:

Note: I'm the maintainer of Spring Cloud Contract.

Upvotes: 2

Related Questions