'What is the best practice to Mock the whole API

Input:
We have API-A and API-B. API-A sends some requests to API-B and do some work with received data.
We have unit tests that verify work of methods that doing this, but these are unit tests. We need to do functional testing of whole API-A but so that it will be independent on API-B's state. E.g. if API-B is under maintenance all tests from API-A will fail due to dependency. To avoid this I want to create Mock API-B.

My idea was that in API-B we can create mock controllers and inject it based on environment where we deploy.
In this case if we want to test E2E flow we deploy API-A that targets to real API-B, if we need to test functional part of API-A, we deploy it with target Mock API-B.

However this approach may has some cons, like if we want to get rid of dependencies from third parties services that we can not control.
So is there any best practice on testing the API without external dependencies?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source