The best practice depends on the type of test, but keep in mind that mock is the answer to many problems.
You have the A microservice and the B microservice. As B uses A , you create an Am microservice, which is the A service mock . It has the same service A API and maybe an extra API to be configured, but its implementation is just a mock.
The unit tests will use only isolated classes of the module being tested and most dependents are mockada (mockar everything blindly can be bad, as I explain in this answer , you need to know how to dosage).
B service acceptance testing or integration B service needs to be uploaded. So when the B service integrates with A , who will be responding will actually be Am .
An example, taking into account your specific case, would look something like this:
- The A service offers the
cadastrarUsuario
and login
APIs.
-
cadastrarUsuario
gets the user name and password and returns a token or an error.
-
login
gets the username and password and returns true or false.
- The Am service has the same API as the A service.
- The A service keeps everything in a database that has long-term persistence and can connect with other services internally. The Am service keeps everything in memory while it is running, connects to nothing, and loses all of your data once it is turned off.
- The B service is responsible for enrolling users.
- The tests of the B service requiring the entire B service running, also go up the Am service.
Note that the only thing that is required for this to work is A and Am should have the same API. However, sometimes (not always) it may be helpful if you make A and Am as extensively as possible, the same code, to access the database.
In particular, mock is very important when you are going to integrate with an external service (imagine the A service to be an API made available by a third party for which you have no control or knowledge about the Implementation). In this case, it is very important that you build an Am service in order to build and test your B service.
Finally, there are scenarios where you will need to do integration testing with the B and A services, without mocks. The ideal is to avoid them as much as possible and minimize them whenever possible, but there may be situations in which avoiding this can be difficult, costly or unfeasible.