hamvocke / spring-testing

A Spring Boot application with lots of test examples
https://www.martinfowler.com/articles/practical-test-pyramid.html
1.09k stars 430 forks source link

Test with real end point for validation #8

Closed sujittripathy closed 6 years ago

sujittripathy commented 6 years ago

Hi, I am wondering if there is a test needed to test with actual REST end point apart from using mocking response (via WireMock) https://api.darksky.net/forecast. If some of the fields from response json has been removed or updated then how to verify with mock? Thanks.

hamvocke commented 6 years ago

That's a legit question.

You're right, if I only ever test against my API fake built with WireMock I'll never notice if the real API has changed until I shoot requests against the real API - in the worst case this means I only spot this issue in production.

I somehow need a way to check if the WireMock fake always behaves like the real thing (Martin Fowler calls this a faithful test double). I could do that as you describe: Fire against the real API in my tests from time to time. Ideally you wouldn't fire requests against a production system in your tests so that you don't accidentally spam it or even DoS the service. Having a dedicated test instance of the real service that you use for testing could help.

Alternatively, you could look into Contract Testing. This mechanism allows you to use a fake API (similar to the one written in WireMock) for testing and automatically ensures that this fake API always behaves like the real thing (is faithful). These tests act on the same layer in your test pyramid as the ones using WireMock. Here's an example of such a test.

You should do one of these (Contract Tests or sending requests against a real instance of the API in one part of your test) otherwise you risk that your API fakes diverge from the real thing.

sujittripathy commented 6 years ago

👍Thanks for the detailed response. In contract test of public API (which I am currently working on for a Payment card), the limit from client side I can verify the contact but the publisher isn't going to verify across all consumers, unlike private APIs which is within the company. I believe for public API only one side contact test is fine?