Best practice depends on the type of test, but it should be borne in mind that the mock is the answer to many problems.
You have the microservice To and the microservice B. Like B uses To, you create a microservice Am, which is the mock of the service To. It has the same API as the A service and maybe an extra API to configure, but its implementation is just a mock.
Unit tests will only use isolated classes of the module being tested and most dependents are mocked (mocking everything blindly can be bad, as I explain in that reply, it is necessary to know how to).
Already in microservice acceptance or integration tests B, should go up, beyond the service B, the service Am. So when the service B integrate with the To, who will be answering in truth will be the Am.
An example, taking into account your specific case, would be something like this:
- The service To offers to Apis
cadastrarUsuario
and login
.
cadastrarUsuario
receives the user name and password and returns a token or an error.
login
receives the user name and password and returns true or false.
- The service Am has the same API as the service To.
- The service To keeps everything in a database with long-term persistence and can connect with other services internally. The service Am keeps everything in memory while running, does not connect to anything else and loses all its data as soon as it is shut down.
- The service B is responsible for conducting user registrations.
- The tests of the service B in need of service B entire running, also go up the service Am.
Note that the only thing that is necessary for this to work is that To and Am should have the same API. However, sometimes (not always) it may be useful for you to have To and Am have in the largest possible extent, the same code, exchanging only an internal component to access the database.
In particular, the mock is very important when you will integrate with an external service (imagine that the service To is 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 you build a service Am to build and test your service B.
Finally, there are scenarios where you will need to do integration tests with the services B and To, mockless. The ideal is to avoid them to the maximum and minimize them whenever possible, but there may be situations where avoiding this can be difficult, costly or impractical.
It’s more or less that, one of the reasons I don’t use both things, a lot of added complexity for little benefit. It has a good side, it’s fashionable.
– Maniero
But which solution do you use?
– Bruno Roberto
A simple one that doesn’t involve complicating your code, your architecture to stay fashionable. Do you need these things? Almost nobody needs them. But they talk so much that people feel obligated to use it. Almost nobody needs it. TDD done wrong, and almost everyone does, it’s only downside, MS in makes little sense, because either you have several micro applications naturally and don’t need to think too much about what to do, or MS adds too much complexity for little or no benefit. What alias your huge team should know how to handle it. Doesn’t it work on a team like this? It doesn’t make sense to use these things.
– Maniero
Hello Bruno! I don’t quite understand how the TDD enters this scenario yours. What kind of test are you doing: unit, integration, other? Tests depend on the other micro service in the air??
– Dherik
@Maniero can I give you some feedback? I saw you in other comments and responses "venting" a little about these and other subjects. I almost always agree with you, but I think this actually deviates from the purpose of the question and confuses users. I hope you take the feedback :), then remove here the comment
– Dherik
@Dherik yes, quiet, but what do you think I should do, just be quiet, like everyone else does, and let the person get stressed out?
– Maniero
@Dherik, I am testing web service, I mean, I am consuming the url s of the service B. However, to use the service B, I need registered users in the base, these users are registered with the service A.
– Bruno Roberto
@Maniero, the warning can continue, I believe only form could change. A tip would be to start the reply/comment by responding to the author and, at the end, adding the warning with the necessary details (since you want to give this message), preferably knowing her context: has she started the project now or not? Still time to change?. Many users come to the question with the same problem and want to see the solution first. For example, imagine that for any difficulty with React the first answer starts by talking to reflect its use and think about using only pure JS?
– Dherik
@Dherik but I answered nothing, I questioned in comment. If I wanted to reply I would have posted a reply, so I just commented, for this serves the comments.
– Maniero
@Maniero, but even in the commentary I think this creates some confusion. But I have an example answer too, in a user’s topic asking if his class UML was correct. He comments that he wanted to get out of the procedural form of code and think of OOP. Link: https://answall.com/a/311979/4492
– Dherik
@Dherik and what is your proposal? And what is the problem in the question?
– Maniero
@Maniero, I commented in my previous comment on the suggestion, but I took the liberty of answering the question of the link cited earlier to demonstrate what I meant :).
– Dherik
@Dherik is that your proposal only makes sense if it is an answer, I just commented. I then see your answer. Usually I go reading and answering the question sentence by sentence, if one puts at the beginning something I think I should talk about, I answer that at the beginning.
– Maniero